LearnWave
Learn it. Master it.

Introduction to Structured Query Language (SQL)

Querying with SELECT

Write a SELECT with a condition.

SELECT retrieves data. Its basic form: SELECT attribute1, attribute2, ... FROM table_name WHERE condition;

  • SELECT * (an asterisk instead of column names) shows all columns.
  • Without WHERE, every row is shown. The book's examples use an EMPLOYEE table (EmpNo, Ename, Salary, Bonus, DeptId).

Column aliases with AS: SELECT Ename AS Name, Salary * 12 AS 'Annual Salary' FROM EMPLOYEE; renames the columns in the output only. An alias with a space must be in quotes. The alias does not add a column to the table.

DISTINCT removes repeated values from the output: SELECT DISTINCT DeptId FROM EMPLOYEE; lists each department once (5 departments in the book's table).

WHERE conditions:

  • Relational operators: =, <, <=, >, >=, and != (not equal).
  • Logical operators: AND (both true), OR (either true), NOT (reverses a condition). For example: WHERE Salary > 5000 AND DeptId = 'D04'; WHERE NOT DeptId = 'D03'.
  • Text values in conditions go in quotes.

BETWEEN gives a range that includes both ends: WHERE Salary BETWEEN 20000 AND 50000 is the same as Salary >= 20000 AND Salary <= 50000.

Example (new): SELECT Ename, Salary FROM EMPLOYEE WHERE DeptId = 'D02' OR Salary < 15000; lists the names and salaries of everyone in department D02, plus anyone anywhere earning under 15000.

SELECT what, FROM where, WHERE which.

Practise this lesson

Questions that get harder as you improve, a Tutor that helps without giving the answer away, and progress you can see. Free for every learner.

No account, no email address. Opens the real product.