Jul 18, 2024
SELECT
SELECT [list of attributes] FROM [tables] [WHERE condition]
SELECT
: Command used to retrieve datalist of attributes
: Columns to be fetchedtables
: Source tables (can be one or multiple)WHERE condition
: Optional filter conditionSELECT * FROM student;
SELECT Sid, Sname, Sh FROM student;
SELECT * FROM student WHERE Sh > 15;
SELECT
is equivalent to the relational algebra projection for specified columns.SELECT * FROM student WHERE Sh > 50 AND Sh < 45;
AND
, OR
, NOT
<
, <=
, >
, >=
, =
, !=
!=
or <>
SELECT * FROM student WHERE Sh BETWEEN 15 AND 45;
BETWEEN
operator can be used with text and datesSELECT * FROM student WHERE Sh IN (25, 26);
SELECT * FROM student WHERE Sh NOT IN (25, 26);
LIKE
for text searching
%
: Zero or more characters_
: Single characterSELECT * FROM student WHERE Sname LIKE 'J%';
SELECT * FROM student WHERE Sname IS NULL;
SELECT * FROM student WHERE Sname IS NOT NULL;
SELECT * FROM student ORDER BY Sh DESC, Sid ASC;
SELECT * FROM student, department;
SELECT * FROM student s INNER JOIN department d ON s.dnum = d.dnum;
SELECT * FROM department AS d INNER JOIN student AS s ON d.dnum = s.dnum;
SELECT * FROM student AS s LEFT JOIN department AS d ON s.dnum = d.dnum;
SELECT * FROM student AS s RIGHT JOIN department AS d ON s.dnum = d.dnum;
SUM
, AVG
, MIN
, MAX
, COUNT
SELECT COUNT(*) FROM student;
SELECT AVG(Sh) FROM student;
SELECT COUNT(Sname) FROM student WHERE Sh > 25;