CS61A Discussion 12
SQL
Slides: albertxu.xyz/teaching/cs61a/
CS61A Discussion 12 SQL Albert Xu Slides: - - PowerPoint PPT Presentation
CS61A Discussion 12 SQL Albert Xu Slides: albertxu.xyz/teaching/cs61a/ Why SQL? a declarative language - instead of implementing a specific procedure to make a table, we give SQL queries that are handled for us. Why SQL? a declarative language
Slides: albertxu.xyz/teaching/cs61a/
a declarative language - instead of implementing a specific procedure to make a table, we give SQL queries that are handled for us.
a declarative language - instead of implementing a specific procedure to make a table, we give SQL queries that are handled for us. …this is implemented for us in the SQL engine we’re using, and something that we don’t have to worry about.
a declarative language - instead of implementing a specific procedure to make a table, we give SQL queries that are handled for us. …this is implemented for us in the SQL engine we’re using, and something that we don’t have to worry about.
a declarative language - instead of implementing a specific procedure to make a table, we give SQL queries that are handled for us. …this is implemented for us in the SQL engine we’re using, and something that we don’t have to worry about. SQL ver.
a declarative language - instead of implementing a specific procedure to make a table, we give SQL queries that are handled for us. …this is implemented for us in the SQL engine we’re using, and something that we don’t have to worry about. When it comes to storing data, we need tables! One of the most popular ways of working with tables is SQL. SQL ver.
SQLite
equality is mostly the same except = vs. ==
SELECT * FROM records WHERE TITLE = “Programmer” select * from records where title = “Programmer” SeLeCt * FrOm records WhErE tItLe = “Programmer”
these are all ok! why?
SeLeCt * FrOm records WhErE tItLe == “Programmer”
this is not
CREATE TABLE records AS SELECT “Ben Bitdiddle” AS Name, “Computer” AS Division, “Wizard” AS Title, 60000 AS Salary, “Oliver Warbucks” AS Supervisor UNION SELECT “Alyssa P Hacker”, “Computer” , “Programmer”, 40000, “Ben Bitdiddle” UNION SELECT “Cy D Fect” , “Computer” , “Programmer”, 35000, “Ben Bitdiddle” UNION SELECT “Lem E Tweakit” , “Computer” , “Technician”, 25000, “Ben Bitdiddle” UNION . . . SELECT “Robert Cratchet”, “Accounting”, “Scrivener” , 18000, “Eben Scrooge”;
SELECT name, division FROM records WHERE supervisor = “Ben Bitdiddle”;
SELECT name, division FROM records WHERE supervisor = “Ben Bitdiddle”;
SELECT name, division FROM records WHERE supervisor = “Ben Bitdiddle”;
SELECT name, division FROM records WHERE supervisor = “Ben Bitdiddle”;
SELECT name, division FROM records WHERE supervisor = “Ben Bitdiddle”;
so we get this!
table1 table2
table1 table2
SELECT * FROM table1, table2;
*implicitly performs an inner join
table1 table2
SELECT * FROM table1, table2;
*implicitly performs an inner join
SELECT <cols> FROM <tables> WHERE <condition>; (1) Where is my data coming from?
SELECT <cols> FROM <tables> WHERE <condition>; (1) Where is my data coming from?
SELECT <cols> FROM <tables> WHERE <condition>; (1) Where is my data coming from? (2) Which joins make sense?
SELECT <cols> FROM <tables> WHERE <condition>; (1) Where is my data coming from? (2) Which joins make sense? (3) Anything else to filter on?
SELECT <cols> FROM <tables> WHERE <condition>; (1) Where is my data coming from? (4) Which columns to keep? (2) Which joins make sense? (3) Anything else to filter on?
…allows us to combine rows to get a single value, in some manner.
…allows us to combine rows to get a single value, in some manner.
Useful aggregation functions: MAX MIN COUNT SUM
…allows us to combine rows to get a single value, in some manner.
SELECT COUNT(*) FROM records SELECT MAX(Salary), MIN(Division) FROM records
Useful aggregation functions: MAX MIN COUNT SUM
SELECT COUNT(*) FROM records
SELECT COUNT(*) FROM records
8
SELECT MAX(Salary), MIN(Division) FROM records
150000 Accounting
SELECT MAX(Salary), MIN(Division) FROM records
GROUP BY is an optional keyword we can add to our SELECT statement, which creates groups. When aggregation is performed on a grouped table, we get an aggregate value for each group.
SELECT COUNT(*) FROM records GROUP BY Division
GROUP BY is an optional keyword we can add to our SELECT statement, which creates groups. When aggregation is performed on a grouped table, we get an aggregate value for each group.
SELECT MAX(Salary), MIN(Division) FROM records GROUP BY Supervisor HAVING COUNT(*) >= 2
SELECT COUNT(*) FROM records GROUP BY Division
SELECT COUNT(*) FROM records GROUP BY Division
accounting administration computer
SELECT COUNT(*) FROM records GROUP BY Division
2 1 5
accounting administration computer
SELECT MAX(Salary), MIN(Division) FROM records GROUP BY Supervisor HAVING COUNT(*) >= 2
SELECT MAX(Salary), MIN(Division) FROM records GROUP BY Supervisor HAVING COUNT(*) >= 2
alyssa ben eben
SELECT MAX(Salary), MIN(Division) FROM records GROUP BY Supervisor HAVING COUNT(*) >= 2
30000 Computer 40000 Computer 18000 Accounting 150000 Administration
alyssa ben eben
HAVING is a keyword we can attach to GROUP BY which filters out groups!
SELECT MAX(Salary), MIN(Division) FROM records GROUP BY Supervisor HAVING COUNT(*) >= 2
30000 Computer 40000 Computer 18000 Accounting 150000 Administration
alyssa ben eben
HAVING is a keyword we can attach to GROUP BY which filters out groups!
SELECT MAX(Salary), MIN(Division) FROM records GROUP BY Supervisor HAVING COUNT(*) >= 2
30000 Computer 40000 Computer 18000 Accounting 150000 Administration
alyssa ben eben
HAVING is a keyword we can attach to GROUP BY which filters out groups!
SELECT MAX(Salary), MIN(Division) FROM records GROUP BY Supervisor HAVING COUNT(*) >= 2
30000 Computer 40000 Computer 150000 Administration
alyssa ben
HAVING is a keyword we can attach to GROUP BY which filters out groups!
SELECT MAX(Salary), MIN(Division) FROM records GROUP BY Supervisor HAVING COUNT(*) >= 2
30000 Computer 40000 Computer 150000 Administration
alyssa ben
Discuss: Why can’t we just use WHERE?
…allows us to combine rows to get a single value, in some manner.
SELECT COUNT(*) FROM records SELECT MAX(Salary), MIN(Division) FROM records
Useful aggregation functions: MAX MIN COUNT SUM
Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/