SQL SELECT

Master the fundamental SQL SELECT statement to retrieve and analyze your data effectively. Learn about column selection, aliases, calculations, and more through hands-on examples.

What is SELECT?

The SELECT statement retrieves data from database tables. It's the foundation of all SQL queries.

Key Concepts:

  • SELECT specifies which columns to retrieve
  • FROM indicates which table(s) to query
  • You can select specific columns or all columns (*)
  • Results are returned as a table of rows

Basic Syntax:

SELECT column1, column2, ... FROM table_name;
SELECT * FROM table_name;  -- Get all columns

Common Use Cases:

  • Retrieving customer information
  • Generating reports
  • Analyzing data patterns
  • Building API responses

Basic SELECT Query

Let's start with a simple query to get customer data.

Write a query that:

  • Retrieves all columns (*)
  • From the customers table
  • Returns all rows

Tips:

  • Use SELECT * to get all columns
  • Always specify the table name after FROM
  • End your query with a semicolon (optional in SQLite)
  • Consider performance with large tables

Available Tables:

customers

Outerbase Query Editor

Selecting Specific Columns

Often, you only need certain columns, not the entire row.

Write a query that gets:

  • Customer names (name column)
  • Email addresses (email column)
  • From the customers table

Benefits of Selecting Specific Columns:

  • Reduces data transfer
  • Improves query performance
  • Makes results more focused
  • Saves client memory

Best Practice: Only select the columns you actually need.

Available Tables:

customers

Outerbase Query Editor

Column Aliases

Aliases give columns more readable or specific names in the results.

Write a query that:

  • Gets the product name as "Item"
  • Gets the price as "Cost"
  • From the products table

Use Cases for Aliases:

  • Making results more readable
  • Avoiding column name conflicts
  • Clarifying calculated fields
  • Matching API requirements

Syntax: Use AS or just a space between the column and alias.

Available Tables:

products

Outerbase Query Editor

Calculated Fields

You can perform calculations in your SELECT statement.

Write a query that gets:

  • Product name
  • Price
  • Price with 20% discount (price * 0.8)
  • From the products table

Common Calculations:

  • Arithmetic operations (+, -, *, /)
  • Date calculations
  • String concatenation
  • Conditional logic

Note: Calculated fields can also have aliases for clarity.

Available Tables:

products

Outerbase Query Editor

DISTINCT Values

The DISTINCT keyword removes duplicate values from results.

Write a query that gets:

  • Unique countries
  • From the customers table
  • Using DISTINCT

Use Cases for DISTINCT:

  • Finding unique categories
  • Removing duplicate entries
  • Analyzing data distribution
  • Cleaning data

Note: DISTINCT considers NULL as a value and will return it once if present.

Available Tables:

customers

Outerbase Query Editor

Combining Concepts

Let's combine multiple concepts in one query.

Write a query that:

  • Gets unique product categories
  • Shows the average price per category
  • Aliases the average as "avg_price"
  • Rounds the average to 2 decimal places

Advanced Techniques:

  • Combining DISTINCT with calculations
  • Using built-in functions (ROUND)
  • Creating meaningful aliases
  • Organizing complex queries

This brings together several concepts we've learned.

Available Tables:

products

Outerbase Query Editor

Space, at your fingertips
astronaut

What will you discover?