-
Notifications
You must be signed in to change notification settings - Fork 17
WHERE
Filtering a table by a predicate
The where clause in a select query is used to reduce the resulting rows by a condition.
For example, using the following table:
employee
+------+--------+-----------------+
| id | name | department_id |
|------+--------+-----------------|
| 1 | Josh | 1 |
| 2 | Ruth | 2 |
| 3 | Greg | 5 |
| 4 | Pat | 1 |
+------+--------+-----------------+
We can find all employees who have department_id of 1 by using a where clause.
In PostgreSQL:
SELECT * FROM employee WHERE department_id = 1;
+------+--------+-----------------+
| id | name | department_id |
|------+--------+-----------------|
| 1 | Josh | 1 |
| 4 | Pat | 1 |
+------+--------+-----------------+
In SQLToy:
employee = FROM('employee');
result = WHERE(employee, (c) => c["department_id"] === 1);
table(result);
Results:
ββββββ¬βββββββββ¬ββββββββββββββββ
β id β name β department_id β
ββββββΌβββββββββΌββββββββββββββββ€
β 1 β Josh β 1 β
β 4 β Pat β 1 β
ββββββ΄βββββββββ΄ββββββββββββββββ
The where clause is applied after any join operations according to the SQL order of operations. Therefore the where clause can refer to columns from any joined tables. Join will prefix the table name onto the column name and the where clause should use the table.column
syntax to reference the column.
For example this SQL query:
SELECT * FROM employee
JOIN employee_club ON employee_club.a = employee.id
JOIN club ON club.id = employee_club.b
WHERE salary > 150000 AND club.name = 'Cat Lovers';
Can be executed in SQLToy like:
const employee = FROM('employee');
const employee_club = FROM('employee_club');
const club = FROM('club');
result = INNER_JOIN(employee, employee_club, (c) => c["employee_club.a"] === c["employee.id"]);
result = INNER_JOIN(result, club, (c) => c["club.id"] === c["employee_club.b"]);
result = WHERE(result, r => r['employee.salary'] > 150000 && r['club.name'] === 'Cat Lovers');
table(result);
Producing the following output:
βββββββββββββββ¬ββββββββββββββββ¬ββββββββββββββββββ¬βββββββββββββββββββββββββ¬ββββββββββββββββββ¬βββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββ¬βββββββββββββββββββ¬βββββββββββββββββββββ
β employee.id β employee.name β employee.salary β employee.department_id β employee.status β employee_club.a β employee_club.b β club.id β club.name β
βββββββββββββββΌββββββββββββββββΌββββββββββββββββββΌβββββββββββββββββββββββββΌββββββββββββββββββΌβββββββββββββββββββββββββββΌβββββββββββββββββββββββββββΌβββββββββββββββββββΌβββββββββββββββββββββ€
β 2 β Jane β 160000 β 2 β active β 2 β 1 β 1 β Cat Lovers β
β 4 β Elliot β 180000 β 1 β active β 4 β 1 β 1 β Cat Lovers β
βββββββββββββββ΄ββββββββββββββββ΄ββββββββββββββββββ΄βββββββββββββββββββββββββ΄ββββββββββββββββββ΄βββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββ΄βββββββββββββββββββ΄βββββββββββββββββββββ
The where clause references two columns from joined tables: employee.salary
and club.name
, using their values to filter the results.
Next section: GROUP_BY
Learn SQL by implementing a SQL database in Javascript