CS61A Discussion 12 SQL Albert Xu Slides: - - PowerPoint PPT Presentation

cs61a discussion 12
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS61A Discussion 12

SQL

Slides: albertxu.xyz/teaching/cs61a/

Albert Xu

slide-2
SLIDE 2

Why SQL?

a declarative language - instead of implementing a specific procedure to make a table, we give SQL queries that are handled for us.

slide-3
SLIDE 3

Why SQL?

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.

slide-4
SLIDE 4

Why SQL?

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.

slide-5
SLIDE 5

Why SQL?

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.

slide-6
SLIDE 6

Why SQL?

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.

slide-7
SLIDE 7

SQLite

  • The specific implementation we’re using is called

SQLite

  • It’s case insensitive for keywords and column names!
  • White space is also fine, just like in Scheme!
  • Boolean operators (AND, NOT, OR) are the same,

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

slide-8
SLIDE 8

Creating Tables

slide-9
SLIDE 9

Creating Tables

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”;

slide-10
SLIDE 10

Understanding SELECT

SELECT name, division FROM records WHERE supervisor = “Ben Bitdiddle”;

slide-11
SLIDE 11

SELECT name, division FROM records WHERE supervisor = “Ben Bitdiddle”;

Understanding SELECT

slide-12
SLIDE 12

SELECT name, division FROM records WHERE supervisor = “Ben Bitdiddle”;

Understanding SELECT

slide-13
SLIDE 13

SELECT name, division FROM records WHERE supervisor = “Ben Bitdiddle”;

Understanding SELECT

slide-14
SLIDE 14

SELECT name, division FROM records WHERE supervisor = “Ben Bitdiddle”;

so we get this!

Understanding SELECT

slide-15
SLIDE 15

Joins

table1 table2

slide-16
SLIDE 16

Joins

table1 table2

SELECT * FROM table1, table2;

*implicitly performs an inner join

slide-17
SLIDE 17

Joins

table1 table2

SELECT * FROM table1, table2;

*implicitly performs an inner join

slide-18
SLIDE 18

Thinking with Joins

SELECT <cols> FROM <tables> WHERE <condition>; (1) Where is my data coming from?

slide-19
SLIDE 19

Thinking with Joins

SELECT <cols> FROM <tables> WHERE <condition>; (1) Where is my data coming from?

slide-20
SLIDE 20

Thinking with Joins

SELECT <cols> FROM <tables> WHERE <condition>; (1) Where is my data coming from? (2) Which joins make sense?

slide-21
SLIDE 21

Thinking with Joins

SELECT <cols> FROM <tables> WHERE <condition>; (1) Where is my data coming from? (2) Which joins make sense? (3) Anything else to filter on?

slide-22
SLIDE 22

Thinking with Joins

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?

slide-23
SLIDE 23

Aggregation

…allows us to combine rows to get a single value, in some manner.

slide-24
SLIDE 24

Aggregation

…allows us to combine rows to get a single value, in some manner.

Useful aggregation functions: MAX MIN COUNT SUM

slide-25
SLIDE 25

Aggregation

…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

slide-26
SLIDE 26

Aggregation (cont.)

SELECT COUNT(*) FROM records

slide-27
SLIDE 27

Aggregation (cont.)

SELECT COUNT(*) FROM records

8

slide-28
SLIDE 28

Aggregation (cont.)

SELECT MAX(Salary), MIN(Division) FROM records

slide-29
SLIDE 29

Aggregation (cont.)

150000 Accounting

SELECT MAX(Salary), MIN(Division) FROM records

slide-30
SLIDE 30

Now With Groups

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.

slide-31
SLIDE 31

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

Now With Groups

slide-32
SLIDE 32

Now With Groups (cont.)

SELECT COUNT(*) FROM records GROUP BY Division

slide-33
SLIDE 33

Now With Groups (cont.)

SELECT COUNT(*) FROM records GROUP BY Division

accounting administration computer

slide-34
SLIDE 34

Now With Groups (cont.)

SELECT COUNT(*) FROM records GROUP BY Division

2 1 5

accounting administration computer

slide-35
SLIDE 35

Now With Groups (cont.)

SELECT MAX(Salary), MIN(Division) FROM records GROUP BY Supervisor HAVING COUNT(*) >= 2

slide-36
SLIDE 36

Now With Groups (cont.)

SELECT MAX(Salary), MIN(Division) FROM records GROUP BY Supervisor HAVING COUNT(*) >= 2

alyssa ben eben

  • liver
slide-37
SLIDE 37

Now With Groups (cont.)

SELECT MAX(Salary), MIN(Division) FROM records GROUP BY Supervisor HAVING COUNT(*) >= 2

30000 Computer 40000 Computer 18000 Accounting 150000 Administration

alyssa ben eben

  • liver
slide-38
SLIDE 38

Now With Groups (cont.)

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

  • liver
slide-39
SLIDE 39

Now With Groups (cont.)

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

  • liver
slide-40
SLIDE 40

Now With Groups (cont.)

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

  • liver
slide-41
SLIDE 41

Now With Groups (cont.)

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

  • liver

Discuss: Why can’t we just use WHERE?

slide-42
SLIDE 42

An Example of Aggregation with Groups

…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

slide-43
SLIDE 43

thanks for coming!

Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/

have a good week :)