solulubeta
Sign in
SQL / Reading a table
01.01 · Lesson

Reading a table

SELECT, WHERE and ORDER BY: picking columns, filtering rows and sorting them.

3 min Free

This lesson covers SELECT, WHERE, AND / OR, BETWEEN, ORDER BY. Read it, then solve questions 1–10 below it in the sidebar. Every query runs on a real PostgreSQL database inside your browser.

How a query reads

A query names the columns you want, the table they come from, and optionally which rows to keep and how to sort them:

SELECT columns
FROM table
WHERE condition
ORDER BY column;

SQL keywords are not case-sensitive (select = SELECT), but text values are: 'Stationery' is not 'stationery'. End each statement with ;.

Choosing columns

SELECT * returns every column in table order. Listing names returns only those, in the order you list them. You can also compute new columns and name them with AS:

SELECT product_name, unit_price, unit_price * 2 AS price_for_two
FROM products;

Filtering rows with WHERE

WHERE keeps the rows for which the condition is true. Comparison operators: =, <> (or !=), <, <=, >, >=.

  • Numbers are written plainly: unit_price > 1000 (no quotes, no commas).
  • Text and dates go in single quotes: city = 'Mumbai', sale_date = '2024-01-13'.
  • Combine conditions with AND / OR; use brackets when you mix them.
SELECT customer_name, city, joined_on
FROM customers
WHERE city = 'Mumbai' OR joined_on >= '2024-01-01';

Ranges and lists

BETWEEN a AND b is inclusive at both ends. IN (…) tests membership in a list. NOT flips either.

SELECT sale_id, quantity_sold
FROM sales
WHERE quantity_sold BETWEEN 2 AND 5
  AND product_id IN (106, 108, 112);

Sorting with ORDER BY

ORDER BY col sorts ascending (ASC, the default); add DESC for descending. Several columns sort by the first, then break ties with the next. Without ORDER BY the row order is not guaranteed, so only rely on it when you asked for it.

SELECT customer_name, city
FROM customers
ORDER BY city, customer_name DESC;
Mark this as done when you are finished.

Item 1 of 77 · updated 2026-10-12