Skip to content

SELECT Statement

SELECT retrieves data from one or more tables β€” the most fundamental SQL statement. You specify which columns to return (or * for all), which table(s) to read from, and optionally filter, sort, and limit the results with other clauses (WHERE, ORDER BY, TOP, etc.).

SELECT * FROM Customers; -- every column, every row
SELECT Name, Email FROM Customers; -- specific columns only
SELECT Name AS CustomerName, Email FROM Customers; -- column alias for output
SELECT DISTINCT Country FROM Customers; -- unique values only, duplicates removed
SELECT TOP 10 * FROM Customers ORDER BY CreatedAt DESC; -- first 10 rows, most recent first
SELECT Name, Price, Price * 1.08 AS PriceWithTax FROM Products; -- computed column

Using SELECT * in production application code instead of naming the specific columns you need β€” it pulls unnecessary data over the network, breaks if the table’s columns change order or new ones are added, and can silently return more data than the application expects.

-- Fragile: pulls every column, including ones the app doesn't use,
-- and breaks assumptions if the schema changes later
SELECT * FROM Customers WHERE Id = 42;
-- Explicit, resilient to schema changes, and only transfers needed data
SELECT Id, Name, Email FROM Customers WHERE Id = 42;
  1. What does SELECT DISTINCT Country FROM Customers; return?

    AnswerEach unique value found in the Country column, with duplicates removed.
  2. Why is SELECT * generally discouraged in application code, even though it’s convenient while exploring data?

    AnswerIt transfers unnecessary data, is fragile to schema changes (column additions/reordering), and makes it unclear exactly which columns the calling code actually depends on.
  3. What does SELECT TOP 10 * FROM Customers ORDER BY CreatedAt DESC; return?

    AnswerThe 10 most recently created customers, since it sorts by CreatedAt descending before limiting to the first 10 rows.