- Data Definition Language (DDL)
create table, drop table, etc - Data Manipulation Language (DML)
select, insert, delete, update - Other Commands
indexes, constraints, views, triggers, transactions, authorization, etc
Basic SELECT
Statement
Select A1, A2, ..., An
From R1, R2, ..., Rm
Where condition
example
SELECT name FROM celebs;
SELECT * FROM celebs;
SELECT DISTINCT age FROM celebs;
SELECT * FROM celebs WHERE age >= 20;
SELECT * FROM celebs WHERE name LIKE 'da_id';
SELECT * FROM celebs WHERE name LIKE '%vi%';
SELECT * FROM celebs WHERE name BETWEEN 'a' AND 'j'; -- name begin with [a, j)
SELECT * FROM celebs WHERE age BETWEEN 20 AND 30 AND name = 'david';
SELECT * FROM celebs WHERE age BETWEEN 20 AND 30 OR name = 'david';
SELECT * FROM celebs ORDER BY age DESC;
SELECT * FROM celebs ORDER BY age DESC, name;
SELECT * FROM celebs ORDER BY age ASC LIMIT 3;
SELECT S1.id, S1.name, S1.age, S2.id, S2.name, S2.age
FROM celebs S1, celebs S2
WHERE S1.age = S2.age AND S1.id < S2.id;
集合操作
-- union
SELECT cName AS name FROM College
UNION
SELECT sName AS name FROM Student
SELECT cName AS name FROM College
UNION ALL
SELECT sName AS name FROM Student
ORDER BY name;
-- intersect
SELECT sID FROM Apply WHERE major = 'CS'
INTERSECT
SELECT sID FROM Apply WHERE major = 'EE'
-- 上面的 intersect 与下面的相等 (注意distinct)
SELECT DISTINCT A1.sID
FROM Apply A1, Apply A2
WHERE A1.sID = A2.sID AND A1.major = 'CS' AND A2.major = 'EE'
-- except
SELECT sID FROM Apply WHERE major = 'CS'
EXCEPT
SELECT sID FROM Apply WHERE major = 'EE'
-- 上面的 except 与下面的相等
SELECT sID
FROM Student
WHERE sID IN (SELECT sID FROM Apply WHERE major = 'CS')
AND NOT sID IN (SELECT sID FROM Apply WHERE major = 'EE')
Subquery
常会用到 IN, EXISTS, ALL, ANY
-- subquery in where
SELECT sName
FROM Student
WHERE sID = (
SELECT sID
FROM Apply
WHERE cName = 'MIT' AND major = 'CS')
SELECT sName
FROM Student
WHERE sID IN (SELECT sID FROM Apply WHERE major = 'CS')
SELECT cName, state
FROM College C1
WHERE EXISTS (SELECT * FROM College C2
WHERE C2.state = C1.state AND C1.cName <> C2.cName)
SELECT cName
FROM College C1
WHERE NOT EXISTS (SELECT * FROM College C2
WHERE C2.enrollment > C1.enrollment)
-- subquery in from
SELECT *
FROM (SELECT sID, sName, GPA, GPA*(sizeHS/1000.0) AS scaledGPA
FROM Student) G
WHERE ABS(G.scaledGPA - GPA) > 1.0;
-- subquery in select
JOIN
Join vs Subquery
semantic:
- Take cross-product
- Apply selection/condition
- Apply projection
-- cross join
SELECT DISTINCT s.sName, a.major
FROM Student s, Apply a
WHERE s.sID = a.sID
SELECT Apply.sID, sName, GPA, Apply.cName, enrollment
FROM Apply, Student, College
WHERE Apply.sID = Student.sID AND Apply.cName = College.cName
-- inner join on condition
SELECT DISTINCT sName, major
FROM Student JOIN Apply
ON Student.sID = Apply.sID
SELECT Apply.sID, sName, GPA, Apply.cName, enrollment
FROM (Apply JOIN Student ON Apply.sID = Student.sID)
JOIN College ON Apply.cName = College.cName
-- inner join using attrs
SELECT DISTINCT sName, major
FROM Student JOIN Apply using(sID);
-- outer join
-- Left, Right, Full Outer Join
SELECT sName, sID, cName, major
FROM Student LEFT JOIN Apply using(sID)
-- outer join 可用下面的等效
SELECT sName, Student.sID, cName, major
FROM Student, Apply
WHERE Student.sID = Apply.sID
UNION
SELECT sName, sID, NULL, NULL
FROM Student
WHERE sID NOT IN (SELECT sID FROM Apply);
Aggregation
注意 Aggregation 的操作顺序
Select A1, A2, ..., An
From R1, R2, ..., Rm
Where condition
Group By columns
Having condition
聚合函数只能用在 An 和 Having 的 condition 上
Group by 也可以用 nested query 实现,但 group by 更高效
注意 Where 中的 condition 与 Having 中的 condition 含义上的区别
-- Aggregate functions
SELECT AVG(GPA)
FROM Student
WHERE sID IN (SELECT sID FROM Apply WHERE major = 'CS')
SELECT COUNT(*)
FROM College
WHERE enrollment > 15000;
SELECT COUNT(DISTINCT sID)
FROM Apply
WHERE cName = 'Cornell'
SELECT CS.avgGPA - NonCS.avgGPA
FROM (SELECT AVG(GPA) AS avgGPA FROM Student
WHERE sID IN (
SELECT sID FROM Apply WHERE major='CS')) AS CS,
(SELECT AVG(GPA) AS avgGPA FROM Student
WHERE sID NOT IN (
SELECT sID FROM Apply WHERE major='CS')) AS NonCS
SELECT cName, COUNT(*)
FROM Apply
GROUP BY cName;
SELECT cName
FROM Apply
GROUP BY cName
HAVING COUNT(*) < 5;
SELECT DISTINCT cName
FROM Apply A1
WHERE 5 > (SELECT COUNT(*) FROM Apply A2 WHERE A2.cName = A1.cName)
SELECT cName, major, MIN(GPA), MAX(GPA)
FROM Student, Apply
WHERE Student.sID = Apply.sID
GROUP BY cName, major;
SELECT MAX(mx-mn)
FROM (SELECT cName, major, MIN(GPA) AS mn, MAX(GPA) AS mx
FROM Student, Apply
WHERE Student.sID = Apply.sID
GROUP BY cName, major) M;
NULL
-- null value
SELECT sID, sName, GPA
FROM Student
WHERE GPA > 3.5 OR GPA <= 3.5 OR GPA IS NULL;
Data Modification Statements
Insert
INSERT INTO Table
Values(A1, A2, ..., An)
INSERT INTO Table
Select-Statement
-- example
INSERT INTO Apply
SELECT sID, 'Carnegie Mello', 'CS', null
FROM Student
WHERE sID NOT IN (SELECT sID FROM Apply);
Update
Update Table
Set Attr = Expression
Where Condition
-- example
UPDATE Apply
SET decision = 'Y', major = 'economics'
WHERE cName = 'Carnegie Mellon'
AND sID IN (SELECT sID FROM Student WHERE GPA < 3.6);
UPDATE Student
SET GPA = (SELECT MAX(GPA) FROM Student),
sizeHS = (SELECT MIN(sizeHS) FROM Student);
Delete
Delete From Table
Where Condition
-- example
DELETE FROM Student
WHERE sID IN (
SELECT sID
FROM Apply
GROUP BY sID
HAVING COUNT(DISTINCT major) > 2);
DDL
DROP TABLE IF EXISTS Movies;
DROP TABLE IF EXISTS MovieStar;
CREATE TABLE Movies (
title CHAR(100),
year INT,
length INT,
genre CHAR(10),
studioName CHAR(30),
producer INT,
PRIMARY KEY (title, year)
);
CREATE TABLE MovieStar (
name CHAR(30) PRIMARY KEY,
address VARCHAR(255),
gender CHAR(1) DEFAULT '?',
birthdate DATE
);
ALTER TABLE MovieStar ADD phone CHAR(16) DEFAULT 'unlisted';
ALTER TABLE MovieStar DROP birthdate;
SQL 做查询时,只要按照它的要求,根据顺序一步一步写就行了