~/learn/SQL · Query basics

SELECT queries

Ask a table for the columns you care about.

5 min readlesson 1 of 18selectcolumnsbasics

why this matters

Every report, dashboard and interview question starts with pulling the right columns out of a table. SELECT is the first word of almost every query you will ever write.

idINTEGERnameTEXTcountryTEXTsignup_dateTEXT
1Ada LovelaceUK2023-01-14
2Grace HopperUSA2023-02-02
3Alan TuringUK2023-02-19
4Katherine JohnsonUSA2023-03-05

showing 4 of 8 rows · 4 columns

Mental model

products (8 rows, 4 columns)  ->  SELECT name, price  ->  same 8 rows, only 2 columns.
SELECT never removes rows; it only chooses which columns come back.

A relational database stores data in tables: rows are records, columns are attributes. You read data with a SELECT statement, which always names the columns first and the table second.

sql · editable
loading editor…

Try it: Add the category column to the output.

The star shorthand returns every column. It is handy while exploring, but in real queries you should name the columns you actually need so the result stays stable when the table changes.

sql · editable
loading editor…

Note

SQL keywords are case-insensitive. Writing them in upper case is a convention that makes queries easier to scan.

Common gotcha

Column names are not text. Writing SELECT 'name' FROM products returns the literal word "name" on every row instead of the product names. Quote text values, never column names.

What SELECT does

table products (8 rows × 4 cols)
        │
 SELECT name, price   ← keep 2 columns
        │
result (8 rows × 2 cols)

Pro tip

Avoid SELECT * in real work — list the columns you need. It is faster, and the query still works when someone adds a column.

key takeaways

  • SELECT picks columns, FROM picks the table.
  • AS renames a column in the output.
  • DISTINCT removes duplicate rows.

check your understanding

You want each category listed once. Which query does that?

free practice

Build confidence with a question you can solve without Pro.