Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Basic SQL Syntax  ›  Capstone Project

SELECT — Asking for Data

SELECT 15 minA SELECT query names the columns, the table, the rows to keep and their order, and the database runs FROM and WHERE before it builds the output columns.

SELECT — Asking for Data

Your manager asks: "Which customers do we have in Pune? Alphabetical, please." That sentence already contains a complete query. SELECT is the statement you will write more than any other, and it maps almost word for word onto how people ask questions.

The rule

Which columns, from which table, which rows, in what order.

Each part of that line is one clause:

SELECT name, email          -- which columns
FROM customers              -- from which table
WHERE city = 'Pune'         -- which rows
ORDER BY name;              -- in what order

Only SELECT and FROM are required. Some useful extras:

  • SELECT * returns every column. Handy for exploring; in application code, list the columns you need so the query does not change when the table does.
  • AS renames a column in the output: SELECT price * quantity AS total.
  • DISTINCT removes duplicate result rows: SELECT DISTINCT city FROM customers.
  • ORDER BY price DESC sorts largest first; ASC (the default) sorts smallest first.
  • To return only the first few rows, PostgreSQL, MySQL and SQLite use LIMIT 5; SQL Server uses SELECT TOP 5; the SQL standard spells it FETCH FIRST 5 ROWS ONLY.

The shortcut

You write SELECT first, but the database runs the clauses in a different order: FROM → WHERE → SELECT → ORDER BY. It finds the table, filters rows, then computes the output columns, then sorts.

This one fact explains the most common beginner error. A column alias made in SELECT does not exist yet when WHERE runs, so WHERE total > 1000 fails. It does exist by the time ORDER BY runs, so ORDER BY total works.

Turning the manager's question into SQLStep 1 of 4
  1. Which columns?

    She wants customers, so show name (add email if useful).

Drills

Our customers table has four rows: Asha (Pune), Ravi (Delhi), Meera (Pune), Kabir (Mumbai). Answer fast — trust the rule.

quick drill

How many rows does SELECT DISTINCT city FROM customers; return?

quick drill

Which query lists products from most to least expensive?

quick drill

SELECT name, price * 1.18 AS total FROM products WHERE total > 1000; fails. Why?

quick drill

What does SELECT name FROM customers WHERE city = 'Pune' ORDER BY name; return?

Remember

  • Which columns, from which table, which rows, in what order.
  • Written SELECT-first, run FROM → WHERE → SELECT → ORDER BY.
  • Aliases work in ORDER BY but not in WHERE; SELECT * is for exploring, not for app code.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainFrom a question to a query

Write a SELECT for: 'Show the name and price of every product in the stationery category that costs under 200, cheapest first.' Then explain why you could not filter on an alias defined in the SELECT list.