All courses › Databases, Networks and Security › SELECT and WHERE

SELECT and WHERE

An SQL query fetches data. FROM says which table, WHERE which rows, and SELECT which columns are shown. ORDER BY sorts the result. The database runs the parts in a different order than you write them.

SELECT name FROM Student WHERE age > 20;\texttt{SELECT name FROM Student WHERE age > 20;}fetch names of students over 20
... ORDER BY name;\texttt{... ORDER BY name;}sort the result

Symbols

FROM\texttt{FROM}which table
WHERE\texttt{WHERE}which rows
SELECT\texttt{SELECT}which columns

Example

Salaries 520, 610, 480 and 700 (thousand):

SELECT COUNT(*) FROM Employee WHERE salary > 500\texttt{SELECT COUNT(*) FROM Employee WHERE salary > 500} gives 3.

If you forget WHERE in an UPDATE or DELETE, every row is changed.
Practise relational databases and SQL for free →

← Tables and keys · JOIN and GROUP BY →

Part of Databases, Networks and Security: Relational databases and SQL.