Sign in to save your place.
Query data with SELECT, filter with WHERE, and sort with ORDER BY.
SELECT reads data from tables. Use SELECT * to get all columns, or list specific columns: SELECT Id, Name, Email.
Filter rows with WHERE: = (equals), <> (not equal), > < >= <=. Use LIKE for pattern matching (e.g. Name LIKE 'A%' for names starting with A). Combine conditions with AND, OR, or IN (value1, value2, ...).
ORDER BY column ASC (ascending, default) or DESC (descending). Use TOP N to return only the first N rows (e.g. SELECT TOP 10 * FROM Orders ORDER BY OrderDate DESC).
SELECT specifies columns, WHERE filters rows, ORDER BY sorts the result. TOP N limits how many rows you get.
SELECT (what columns) → FROM (which table) → WHERE (which rows) → ORDER BY (sort order) → TOP (limit rows).
-- All columns SELECT * FROM Customers; -- Specific columns, filtered and sorted SELECT Id, Name, Email FROM Customers WHERE Country = 'UK' AND Name LIKE 'A%' ORDER BY Name ASC; -- Top 5 most recent orders SELECT TOP 5 * FROM Orders ORDER BY OrderDate DESC;
Write queries: (1) SELECT only Name and Email from Customers. (2) WHERE Status = 'Active' and Country IN ('US','UK'). (3) ORDER BY CreatedAt DESC with TOP 10.
Correct! DESC sorts in descending order.
ORDER BY ... DESC sorts the result in descending order (largest first, Z to A).
Correct! WHERE filters rows.
WHERE filters which rows are included in the result; SELECT chooses columns.
TOP without ORDER BY returns an arbitrary set. Add ORDER BY when you mean "the first five." WHERE city = NULL matches nothing. Use IS NULL.
PostgreSQL spells the limiter as LIMIT. SQL Server uses TOP or OFFSET / FETCH. Do not mix the two spellings in one script.
SELECT TOP (5) name, amount FROM sales ORDER BY amount DESC;Try this. Return the five largest amounts with TOP (5) and an ORDER BY. Then find rows whose city is missing.
Which predicate finds a missing city?