Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
SQL: Part 1
Lecture 3
1.18.2016 SQL: Part 1 1
SQL: Part 1 Lecture 3 SQL: Part 1 1.18.2016 1 Wentworth - - PowerPoint PPT Presentation
Wentworth Institute of Technology COMP2670 Databases | Spring 2016 | Derbinsky SQL: Part 1 Lecture 3 SQL: Part 1 1.18.2016 1 Wentworth Institute of Technology COMP2670 Databases | Spring 2016 | Derbinsky Outline 1. Context 2.
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 1
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 2
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
Chamberlin, Donald D., and Raymond F. Boyce. "SEQUEL: A structured English query language." Proceedings of the 1974 ACM SIGFIDET (now SIGMOD) workshop on Data description, access and control. ACM, 1974.
1.18.2016 SQL: Part 1 3
“In this paper we present the data manipulation facility for a structured English query language (SEQUEL) which can be used for accessing data in an integrated relational data
and quantifiers SEQUEL identifies a set of simple operations
equivalent power to the first order predicate calculus. A SEQUEL user is presented with a consistent set of keyword English templates which reflect how people use tables to
compose these basic templates in a structured manner in
as a data base sublanguage for both the professional programmer and the more infrequent data base user.”
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
– For the most part
– Now industry standards: SQL-86, SQL-92, SQL:1999 (-2011) – Various degrees of adoption
– Data Definition (DDL): schema structure – Data Manipulation (DML): add/update/delete – Transaction Management: begin/commit/rollback – Data Control: grant/revoke – Query – Configuration …
Good reference: http://www.w3schools.com/sql
1.18.2016 SQL: Part 1 4
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 5
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 6
SELECT * FROM artist;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 7
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
SELECT * FROM employee;
SELECT FirstName AS fname, LastName AS lname FROM employee;
SELECT *, (UnitPrice*Quantity) AS cost FROM invoiceline;
1.18.2016 SQL: Part 1 8
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 9
SELECT Name FROM artist;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 10
SELECT FirstName, LastName, Address, City, State, PostalCode, Country FROM employee;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 11
SELECT InvoiceId, UnitPrice, Quantity FROM invoiceline;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 12
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 13
Operator Meaning Example
= Equal to InvoiceId = 2 <> Not equal to Name <> 'U2' < or > Less/Greater than UnitPrice < 5 <= or >= Less/Greater than or equal to UnitPrice >= 0.99 LIKE Matches pattern PostalCode LIKE 'T2%' IN Within a set City IN ('Calgary', 'Edmonton') IS or IS NOT Compare to NULL ReportsTo IS NULL BETWEEN Inclusive range (esp. dates) UnitPrice BETWEEN 0.99 AND 1.99
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 14
SELECT BillingCountry FROM invoice WHERE Total>10;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 15
SELECT * FROM track WHERE Name LIKE '%Rock%';
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 16
SELECT FirstName, LastName FROM employee WHERE ( ReportsTo IS NOT NULL ) AND ( City = 'Calgary' );
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
– http://sqlite.org/lang.html
– http://dev.mysql.com/doc/refman/5.0/en/func-op-summary-ref.html
1.18.2016 SQL: Part 1 17
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
Get all German invoices greater than $1, output the city using the column header “german_city” and “total” prepending $ to the total
1.18.2016 SQL: Part 1 18
SELECT BillingCity AS german_city, ( '$' || Total ) AS total FROM invoice WHERE ( BillingCountry = 'Germany' ) AND ( Total > 1 );
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
Get all German invoices greater than $1, output the city using the column header “german_city” and “total” prepending $ to the total
1.18.2016 SQL: Part 1 19
SELECT BillingCity AS german_city, CONCAT( '$', Total ) AS total FROM invoice WHERE ( BillingCountry = 'Germany' ) AND ( Total > 1 );
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 20
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
– Order can be ASC or DESC – Default is ASC
1.18.2016 SQL: Part 1 21
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 22
SELECT * FROM invoice WHERE ( BillingCountry = 'USA' ) AND ( Total >= 10 ) ORDER BY Total DESC, BillingState ASC, BillingCity;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 23
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 24
vs.
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 25
R1 UNION R2 R1 INTERSECT R2 R1 MINUS R2 R2 MINUS R1
R2 R1 R1 R2 R1 R2 R2 R1
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 26
SELECT City AS city FROM customer WHERE Country = 'Canada';
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 27
SELECT City AS city FROM employee WHERE Country = 'Canada';
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 28
SELECT City AS city FROM customer WHERE Country = 'Canada' UNION ALL SELECT City AS city FROM employee WHERE Country = 'Canada';
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 29
SELECT City AS city FROM customer WHERE Country = 'Canada' UNION SELECT City AS city FROM employee WHERE Country = 'Canada';
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
Get all Canadian cities in which employees AND customers live (excluding duplicates) [no MySQL support]
1.18.2016 SQL: Part 1 30
SELECT City AS city FROM customer WHERE Country = 'Canada' INTERSECT SELECT City AS city FROM employee WHERE Country = 'Canada';
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
All Canadian cities in which customers live BUT employees do not (excluding duplicates) [no MySQL support]
1.18.2016 SQL: Part 1 31
SELECT City AS city FROM customer WHERE Country = 'Canada' EXCEPT SELECT City AS city FROM employee WHERE Country = 'Canada';
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 32
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 33
a b x 1 y 2 z 3
ALPHA BETA
c d x i y ii
ALPHA X BETA
Alpha.a Alpha.b Beta.c Beta.d x 1 x i x 1 y ii y 2 x i y 2 y ii z 3 x i z 3 y ii
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 34
a b x 1 y 2 z 3
ALPHA BETA
c d x i y ii
ALPHA X BETA | ALPHA.A = BETA.C
Alpha.a Alpha.b Beta.c Beta.d x 1 x i x 1 y ii y 2 x i y 2 y ii z 3 x i z 3 y ii Alpha.a Alpha.b Beta.c Beta.d x 1 x i y 2 y ii
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 35
Name SSN Phone Address Age GPA Ben Bayer 305-61-2435 555-1234 1 Foo Lane 19 3.21 Chung-cha Kim 422-11-2320 555-9876 2 Bar Court 25 3.53 Barbara Benson 533-69-1238 555-6758 3 Baz Blvd 19 3.25
STUDENT
SSN Class 305-61-2435 COMP355 422-11-2320 COMP355 533-69-1238 MATH650 305-61-2435 MATH650 422-11-2320 BIOL110
CLASS Goal: find the GPA of students in MATH650
Approach: cross all rows in STUDENT with all rows in CLASS and keep the Student(GPA) of those where STUDENT(SSN)=CLASS(SSN) and CLASS(Class)=MATH650
GPA 3.21 3.25
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 36
Name SSN Phone Address Age GPA Ben Bayer 305-61-2435 555-1234 1 Foo Lane 19 3.21 Chung-cha Kim 422-11-2320 555-9876 2 Bar Court 25 3.53 Barbara Benson 533-69-1238 555-6758 3 Baz Blvd 19 3.25
STUDENT
SSN Class 305-61-2435 COMP355 422-11-2320 COMP355 533-69-1238 MATH650 305-61-2435 MATH650 422-11-2320 BIOL110
CLASS Approach: cross all rows in STUDENT with all rows in CLASS and keep the GPA of those where STUDENT(SSN)=CLASS(SSN) and CLASS(Class)=MATH650 SELECT STUDENT.GPA FROM STUDENT INNER JOIN CLASS ON STUDENT.SSN=CLASS.SSN WHERE CLASS.Class='MATH650'; Goal: find the GPA of students in MATH650
GPA 3.21 3.25
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 37
Name SSN Phone Address Age GPA Ben Bayer 305-61-2435 555-1234 1 Foo Lane 19 3.21 Chung-cha Kim 422-11-2320 555-9876 2 Bar Court 25 3.53 Barbara Benson 533-69-1238 555-6758 3 Baz Blvd 19 3.25
STUDENT
SSN Class 305-61-2435 COMP355 422-11-2320 COMP355 533-69-1238 MATH650 305-61-2435 MATH650 422-11-2320 BIOL110
CLASS Goal: find the GPA of students in MATH650 Approach: cross all rows in STUDENT with all rows in CLASS and keep the GPA of those where STUDENT(SSN)=CLASS(SSN) and CLASS(Class)=MATH650 SELECT STUDENT.GPA FROM STUDENT, CLASS WHERE STUDENT.SSN=CLASS.SSN AND CLASS.Class='MATH650'; Soft Joins (older style) intermix row filtration with table join conditions
GPA 3.21 3.25
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 38
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
Row must exist in both tables
Row must at least exist in the table to the left (padded with NULL)
Row must exist at least in the table to the right (padded with NULL)
Row exists in either table (padded with NULL)
1.18.2016 SQL: Part 1 39
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 40
a b x 1 y 2 z 3
ALPHA BETA
c d w
ii
SELECT * FROM Alpha INNER JOIN Beta ON Alpha.a=Beta.c
Alpha.a Alpha.b Beta.c Beta.d y 2 y ii
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 41
a b x 1 y 2 z 3
ALPHA BETA
c d w
ii
SELECT * FROM Alpha LEFT OUTER JOIN Beta ON Alpha.a=Beta.c
Alpha.a Alpha.b Beta.c Beta.d x 1 NULL NULL y 2 y ii z 3 NULL NULL
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 42
a b x 1 y 2 z 3
ALPHA BETA
c d w
ii
SELECT * FROM Alpha RIGHT OUTER JOIN Beta ON Alpha.a=Beta.c
Alpha.a Alpha.b Beta.c Beta.d y 2 y ii NULL NULL w
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 43
a b x 1 y 2 z 3
ALPHA BETA
c d w
ii
SELECT * FROM Alpha FULL OUTER JOIN Beta ON Alpha.a=Beta.c
Alpha.a Alpha.b Beta.c Beta.d x 1 NULL NULL y 2 y ii z 3 NULL NULL NULL NULL w
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
– Tip: when listing the table name, give it a shortcut
SELECT * FROM table1 t1
– Optional shortcut if joining attribute(s) have same name(s) in both tables
– Particularly full outer, natural
1.18.2016 SQL: Part 1 44
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 45
SELECT * FROM genre INNER JOIN mediatype;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
Get all track information, with the appropriate genre name and media type name, for all jazz tracks where Miles Davis helped compose
1.18.2016 SQL: Part 1 46
SELECT * FROM (track t INNER JOIN mediatype mt ON t.MediaTypeId=mt.MediaTypeId) INNER JOIN genre g ON t.GenreId=g.GenreId WHERE g.Name='Jazz' AND t.Composer LIKE '%Miles Davis%';
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 47
SELECT * FROM artist WHERE Name LIKE 'Black%' ORDER BY Name ASC;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 48
SELECT * FROM artist art INNER JOIN album alb ON art.ArtistId=alb.ArtistId WHERE Name LIKE 'Black%' ORDER BY art.Name ASC, alb.Title ASC;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 49
SELECT * FROM artist art LEFT OUTER JOIN album alb ON art.ArtistId=alb.ArtistId WHERE Name LIKE 'Black%' ORDER BY art.Name, alb.Title;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
Get all artist AND album information for those artists whose name begins with ‘Black’ (do include those without albums!), provide only a single correct ArtistId, sort by artist name, then album title
1.18.2016 SQL: Part 1 50
SELECT art.ArtistId, art.Name, alb.AlbumId, alb.Title FROM artist art LEFT OUTER JOIN album alb ON art.ArtistId=alb.ArtistId WHERE Name LIKE 'Black%' ORDER BY art.Name, alb.Title;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 51
SELECT t.TrackId, t.Name AS tName, t.Composer, t.UnitPrice, a.Title, m.Name AS mName, g.Name AS gName FROM ((track t INNER JOIN album a ON t.AlbumId=a.AlbumId) INNER JOIN mediatype m ON t.MediaTypeId=m.MediaTypeId) INNER JOIN genre g ON t.GenreId=g.GenreId WHERE t.Name='Give Me Novacaine';
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 52
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 53
Name SSN Phone Address Age GPA Ben Bayer 305-61-2435 555-1234 1 Foo Lane 19 3.21 Chung-cha Kim 422-11-2320 555-9876 2 Bar Court 25 3.53 Barbara Benson 533-69-1238 555-6758 3 Baz Blvd 19 3.25
STUDENT
SSN Class 305-61-2435 COMP355 422-11-2320 COMP355 533-69-1238 MATH650 305-61-2435 MATH650 422-11-2320 BIOL110
CLASS Approach: cross all rows in STUDENT with all rows in CLASS and keep the GPA of those where STUDENT(SSN)=CLASS(SSN) and CLASS(Class)=MATH650 SELECT STUDENT.GPA FROM STUDENT INNER JOIN CLASS ON STUDENT.SSN=CLASS.SSN WHERE CLASS.Class='MATH650'; Goal: find the GPA of students in MATH650
GPA 3.21 3.25
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 54
Name SSN Phone Address Age GPA Ben Bayer 305-61-2435 555-1234 1 Foo Lane 19 3.21 Chung-cha Kim 422-11-2320 555-9876 2 Bar Court 25 3.53 Barbara Benson 533-69-1238 555-6758 3 Baz Blvd 19 3.25
STUDENT
SSN Class 305-61-2435 COMP355 422-11-2320 COMP355 533-69-1238 MATH650 305-61-2435 MATH650 422-11-2320 BIOL110
CLASS Approach: cross all rows in STUDENT with all rows in CLASS and keep the GPA of those where STUDENT(SSN)=CLASS(SSN) and CLASS(Class)=MATH650, average result set SELECT AVG(STUDENT.GPA) AS aGPA FROM STUDENT INNER JOIN CLASS ON STUDENT.SSN=CLASS.SSN WHERE CLASS.Class='MATH650'; Goal: find the average GPA of students in MATH650
aGPA 3.23
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
– COUNT(*) = number of rows – COUNT(field) = number of non-NULL values – COUNT(DISTINCT field) = number of distinct values of a field
1.18.2016 SQL: Part 1 55
SELECT MIN(UnitPrice) AS min_price FROM track; SELECT MAX(UnitPrice) AS max_price FROM track; SELECT AVG(UnitPrice) AS avg_price FROM track; SELECT MIN(UnitPrice) AS min_price, MAX(UnitPrice) AS max_price, AVG(UnitPrice) AS avg_price FROM track; SELECT COUNT(*) AS num_tracks FROM track WHERE AlbumId=1; SELECT SUM(UnitPrice) AS total_cost FROM track WHERE AlbumId=1;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
The GROUP BY statement allows you to define subgroups for aggregate
SELECT [DISTINCT] <attribute list> FROM <table list> [WHERE <condition list>] [GROUP BY <attribute list>] [ORDER BY <attribute-order list>]; Example: track price stats by media type
SELECT mt.Name AS media_type, MIN(t.UnitPrice) AS min_price, MAX(t.UnitPrice) AS max_price, AVG(t.UnitPrice) AS avg_price FROM track t INNER JOIN MediaType mt ON t.MediaTypeId=mt.MediaTypeId GROUP BY mt.Name ORDER BY avg_price DESC, mt.Name ASC;
1.18.2016 SQL: Part 1 56
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 57
SELECT mt.Name AS media_type, MIN(t.UnitPrice) AS min_price, MAX(t.UnitPrice) AS max_price, AVG(t.UnitPrice) AS avg_price FROM track t INNER JOIN MediaType mt ON t.MediaTypeId=mt.MediaTypeId GROUP BY mt.Name ORDER BY avg_price DESC, mt.Name ASC; SELECT * FROM track t INNER JOIN MediaType mt ON t.MediaTypeId=mt.MediaTypeId ORDER BY mt.Name ASC;
… GROUP BY
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 58
SELECT BillingCity, BillingState, AVG(Total) AS avg_total, SUM(Total) AS sum_total, COUNT(*) AS ct FROM invoice WHERE BillingCountry='USA' GROUP BY BillingCity, BillingState ORDER BY avg_total DESC, BillingState ASC, BillingCity ASC;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 59
SELECT InvoiceId, SUM(UnitPrice*Quantity) AS total FROM invoiceline GROUP BY InvoiceId ORDER BY total DESC, InvoiceId ASC;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
Generate a ranked list of Queen’s best selling tracks. Display the track id, track name, and album name, along with number of tracks sold, sorted by tracks sold (greatest first), then by track name (alphabetical).
1.18.2016 SQL: Part 1 60
SELECT invoiceline.TrackId, track.Name, album.Title, SUM(invoiceline.Quantity) AS num_sold FROM ((invoiceline INNER JOIN track ON invoiceline.TrackId=track.TrackId) INNER JOIN album ON track.AlbumId=album.AlbumId) INNER JOIN artist ON album.ArtistId=artist.ArtistId WHERE artist.Name='Queen' GROUP BY invoiceline.TrackId ORDER BY num_sold DESC, track.Name ASC;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 61
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
Generate a ranked list of Queen’s best selling tracks. Display the track id, track name, and album name, along with number of tracks sold, sorted by tracks sold (greatest first), then by track name (alphabetical). Only show those tracks that have sold at least twice.
1.18.2016 SQL: Part 1 62
SELECT invoiceline.TrackId, track.Name, album.Title, SUM(invoiceline.Quantity) AS num_sold FROM ((invoiceline INNER JOIN track ON invoiceline.TrackId=track.TrackId) INNER JOIN album ON track.AlbumId=album.AlbumId) INNER JOIN artist ON album.ArtistId=artist.ArtistId WHERE artist.Name='Queen' GROUP BY invoiceline.TrackId HAVING SUM(invoiceline.Quantity)>=2 ORDER BY num_sold DESC, track.Name ASC;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 63
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 64
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 65
SELECT t.* FROM track t WHERE t.AlbumId = ( SELECT a.AlbumId FROM album a WHERE a.Title='Jagged Little Pill' );
Notes
return a single value for the = to make sense
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 66
SELECT t.* FROM track t WHERE t.AlbumId = ( SELECT a.AlbumId FROM album a WHERE a.Title='Jagged Little Pill' ); SELECT t.* FROM track t WHERE t.AlbumId = 6;
Inner Query
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 67
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 68
SELECT t.* FROM track t WHERE t.AlbumId IN ( SELECT alb.AlbumId FROM album alb WHERE alb.ArtistId = ( SELECT art.ArtistId FROM artist art WHERE art.Name='Queen' ) );
Notes
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 69
SELECT t.* FROM track t WHERE t.AlbumId IN ( SELECT alb.AlbumId FROM album alb WHERE alb.ArtistId = ( SELECT art.ArtistId FROM artist art WHERE art.Name='Queen' ) ); SELECT t.* FROM track t WHERE t.AlbumId IN ( SELECT alb.AlbumId FROM album alb WHERE alb.ArtistId = 51 ); SELECT t.* FROM track t WHERE t.AlbumId IN (36, 185, 186);
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
For each artist starting with Santana, get the number of albums, sorted by count (greatest first), then artist (alphabetical)
1.18.2016 SQL: Part 1 70
SELECT art.Name AS artist_name, ( SELECT COUNT(*) FROM album alb WHERE alb.ArtistId=art.ArtistId ) AS album_ct FROM artist art WHERE art.Name LIKE 'Santana%' ORDER BY album_ct DESC, art.Name;
Notes
return a single value for each tuple generated
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 71
SELECT art.Name AS artist_name, ( SELECT COUNT(*) FROM album alb WHERE alb.ArtistId=art.ArtistId ) AS album_ct FROM artist art WHERE art.Name LIKE 'Santana%' ORDER BY album_ct DESC, art.Name; SELECT * FROM artist art WHERE art.Name LIKE 'Santana%';
Correlated- one query per row to fill in album_ct column!
SELECT COUNT(*) FROM album alb WHERE alb.ArtistId=59; =60; …
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
For each artist starting with Santana, get the number of albums, sorted by count (greatest first), then artist (alphabetical)
1.18.2016 SQL: Part 1 72
SELECT artist_name, COUNT(q1.AlbumId) AS album_ct FROM ( SELECT art.ArtistId AS artist_id, art.Name AS artist_name, alb.AlbumId FROM artist art LEFT JOIN album alb ON art.ArtistId=alb.ArtistId WHERE art.Name LIKE 'Santana%' ) q1 GROUP BY artist_id ORDER BY album_ct DESC, artist_name;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 73
SELECT artist_name, COUNT(q1.AlbumId) AS album_ct FROM ( SELECT art.ArtistId AS artist_id, art.Name AS artist_name, alb.AlbumId FROM artist art LEFT JOIN album alb ON art.ArtistId=alb.ArtistId WHERE art.Name LIKE 'Santana%' ) q1 GROUP BY artist_id ORDER BY album_ct DESC, artist_name;
q1
SELECT artist_name, COUNT(q1.AlbumId) AS album_ct FROM q1 GROUP BY artist_id ORDER BY album_ct DESC, artist_name;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 74
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
Find the minimum, maximum, and average number of tracks ordered per customer (across all invoices). Also include the total number of customers.
1.18.2016 SQL: Part 1 75
SELECT MIN(q2.sum_q) AS min_q, MAX(q2.sum_q) AS max_q, AVG(q2.sum_q) AS avg_q, COUNT(*) AS num_customers FROM (SELECT q1.CustomerId, SUM(Quantity) AS sum_q FROM (SELECT i.CustomerId, il.Quantity FROM invoice i NATURAL JOIN invoiceline il ) q1 GROUP BY q1.CustomerId ) q2;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 76
SELECT MIN(q2.sum_q) AS min_q, MAX(q2.sum_q) AS max_q, AVG(q2.sum_q) AS avg_q, COUNT(*) AS num_customers FROM (SELECT q1.CustomerId, SUM(Quantity) AS sum_q FROM (SELECT i.CustomerId, il.Quantity FROM invoice i NATURAL JOIN invoiceline il ) q1 GROUP BY q1.CustomerId ) q2;
q1 q2 … …
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
Find the highest spending customers: get a ranked list of customers (first name, last name) who have spent at least $40, sorted by amount spent (greatest first), then last name, then first name
1.18.2016 SQL: Part 1 77
SELECT * FROM ( SELECT c.FirstName, c.LastName, ( SELECT SUM(i.Total) FROM invoice i WHERE c.CustomerId=i.CustomerId ) AS total_spent FROM customer c) q1 WHERE q1.total_spent>=40 ORDER BY q1.total_spent DESC, q1.LastName ASC, q1.FirstName ASC;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
Create a report of the distribution of tracks into genres. The result set should list each genre by name, the number of tracks of that genre, and the percentage of overall tracks for that genre. The rows should be sorted by the percentage (greatest first), then genre name (alphabetically).
1.18.2016 SQL: Part 1 78
SELECT x.Name AS g_name, x.g_ct AS g_ct, (100.0 * g_ct / ct) AS g_percentage FROM (SELECT *, (SELECT COUNT(*) FROM track t1 WHERE t1.GenreId=g.GenreId) AS g_ct, (SELECT COUNT(*) FROM track t2) AS ct FROM genre g) x ORDER BY g_percentage DESC, g_name ASC;
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 79
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 80
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 81
Wentworth Institute of Technology COMP2670 – Databases | Spring 2016 | Derbinsky
1.18.2016 SQL: Part 1 82