Browse SQL Topics โ–ผ

SELECT

๐Ÿ“‚ Reading Data ๐Ÿ“˜ Lesson 11 of 81

Retrieve specific columns, calculate expressions, eliminate duplicates, and query tables using the SQL SELECT statement.

Imagine opening a massive spreadsheet containing 100,000 customers. Each customer has 35 columns: address, phone number, shipping notes, credit limit, internal IDs, and signup timestamps.

If your marketing team only needs a list of customer names and email addresses to send a newsletter, downloading all 35 columns wastes memory, clogs network bandwidth, and slows down your application.

In SQL (Structured Query Language), SELECT is your surgical tool for reading data. It allows you to specify exactly which columns you want to pull out of a table.


The Anatomy of a Basic Query

A database table is organized into columns (attributes) and rows (individual records).

               TABLE: customers
โ”Œโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ id โ”‚ name          โ”‚ email                โ”‚ city        โ”‚
โ”œโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 1  โ”‚ Alice Johnson โ”‚ alice@example.com    โ”‚ New York    โ”‚
โ”‚ 2  โ”‚ Bob Smith     โ”‚ bob@example.com      โ”‚ London      โ”‚
โ”‚ 3  โ”‚ Carlos Ruiz   โ”‚ carlos@example.com   โ”‚ Madrid      โ”‚
โ””โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The fundamental SQL statement specifies two things:

  1. SELECT: Which columns do you want?
  2. FROM: Which table holds those columns?
SELECT name, city
FROM customers;

Result:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ name          โ”‚ city     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Alice Johnson โ”‚ New York โ”‚
โ”‚ Bob Smith     โ”‚ London   โ”‚
โ”‚ Carlos Ruiz   โ”‚ Madrid   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Notice that SQL returns only the requested two columns, ignoring id and email.


Fetching All Columns: SELECT *

If you want to inspect every single column in a table, SQL provides the asterisk wildcard (*):

SELECT *
FROM customers;

[!TIP] SELECT * is great for quick debugging in your database terminal. However, never use SELECT * in production backend code. If someone later adds a 50 MB profile_picture_blob column to the customers table, every query in your application will suddenly become drastically slower! Always list the exact columns your application needs.


Renaming Columns with Aliases (AS)

Sometimes raw database column names are cryptic (like usr_fname or tx_dt), or you want to format them nicely for an API response. You can rename columns in the output using the AS keyword:

SELECT 
    name AS customer_name,
    city AS location
FROM customers;

Result:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ customer_name โ”‚ location โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Alice Johnson โ”‚ New York โ”‚
โ”‚ Bob Smith     โ”‚ London   โ”‚
โ”‚ Carlos Ruiz   โ”‚ Madrid   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Calculating Values on the Fly

SELECT is not limited to reading raw columnsโ€”it can also perform calculations, mathematical operations, and string transformations directly inside the database engine:

SELECT 
    name,
    salary,
    salary * 12 AS annual_income,
    salary * 0.10 AS estimated_bonus
FROM employees;

The database executes the multiplication for each row in microseconds and returns annual_income as a computed column, saving your application from having to compute it in memory.


Removing Duplicates with DISTINCT

If five customers live in London and ten live in New York, running SELECT city FROM customers; prints โ€œLondonโ€ five times and โ€œNew Yorkโ€ ten times.

To extract only the unique values, add the DISTINCT keyword immediately after SELECT:

SELECT DISTINCT city
FROM customers;

Result:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ city     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ New York โ”‚
โ”‚ London   โ”‚
โ”‚ Madrid   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Common Traps and Gotchas

1. Missing Commas Between Columns

Each column in your SELECT list must be separated by a comma:

-- WRONG: Causes a syntax error or accidentally aliases 'name' as 'city'!
SELECT name city FROM customers;

-- CORRECT:
SELECT name, city FROM customers;

2. Dangling Trailing Commas

Unlike Python, SQL does not allow a comma after the final column before FROM:

-- WRONG: Syntax error near 'FROM'
SELECT name, city, FROM customers;

-- CORRECT:
SELECT name, city FROM customers;

3. Forgetting the Semicolon

In database command lines and migration scripts, SQL statements require a terminating semicolon (;). Without it, the client waits indefinitely for more input.


Next Steps

Master this concept to build a rock-solid foundation. Next, proceed to select-all-columns.

Test your understanding of SELECT

Quick interactive practice questions with instant feedback.

Take Quiz โ†’