SlideShare a Scribd company logo
1 of 17
AN HOUR WITH
DATABASE AND SQL
MARIHACKS LEARNATHON
IRAJ HEDAYATI
RELATIONAL DATABASE
• Entities and their relations
• One-to-one relation
• Many-to-Many
• Many-to-one relation
Mathematics
Emma
Emma
Caleb
Caleb
fills
fills
Seat 1
Seat
2
History
TABLE
• Table or sometimes called Relation
• Each entity is a table
• Sometimes a relation is a table (e.g. many-to-one)
• Each table is a set of Rows (also called Record or Tuple
• Each Row consists a set of Columns (also called
Attributes)
• Key: there are different key definition but we focus only
on Primary Key and Foreign Key
• Schema is structure and design of database (tables and
relations)
ID Name Telephone Age
123456 Caleb 514222345
6
17
456789 Emma 514111987
6
16
Column
Row
Primary Key
Unique for
each row
DESIGN SCHEMA
• For each entity add a table that reflects attributes of that entity
• For each entity select (or add) a unique column as primary key
• Relations:
• One-to-One and Many-to-One and One-to-Many: in the source table (e.g. student)
add a column that holds primary key of destination table (e.g. seat). This is called
Foreign Key)
• Many-to-Many: add a table for this relation. It has Foreign Keys to each entity involved
in this relation (e.g. Student ID and Course ID). It is like breaking it down to two One-
to-Many and Many-to-One relations.
PRACTICE
SYSTEM REQUIREMENTS
• An in-memory database: H2
• Download: http://www.h2database.com/html/download.html
• H2 is JVM based, so you need install Oracle Java 7 or later
• Start using H2:
• http://www.h2database.com/html/tutorial.html#tutorial_starting_h2_console
• Prepare schema:
• https://s3-us-west-2.amazonaws.com/prod-c2g/db/Winter2013/files/social.sql
SQL
A language to query a database
INTRO
• Data Definition Language (DDL): define schema
• Data Manipulation Language (DML):
• INSERT
• UPDATE
• SELECT
INSERT
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
INSERT INTO highschooler (id, name, grade) VALUES (1510, 'Jordan', 9);
INSERT INTO highschooler VALUES (1510, 'Jordan', 9);
Both are the same. Because we provided values for each column and they are in the
same order.
UPDATE
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
UPDATE highschooler
SET name='Caleb'
WHERE id=1468
SELECT
SELECT column1, column2, column3, …
FROM table1
WHERE c1 AND c2 OR c3
• FROM: which table?
• WHERE: filter rows
• SELECT: what to return?
• Column names
• A function
• STAR
SELECT
• SELECT * FROM highschooler;
• SELECT name FROM highschooler WHERE grade=9;
• SELECT COUNT(*) FROM highschooler WHERE grade=10;
• SELECT *
FROM highschooler
WHERE grade>=10 AND (name LIKE 'A%' OR id>1700)
• SELECT * FROM highschooler ORDER BY name;
• SELECT grade, COUNT(*) FROM highschooler GROUP BY grade;
SELECT (JOIN)
• Join operation is a cartesian product
(also called FULL OUTER JOIN)
• INNER JOIN only return those that
match
SELECT *
FROM highschooler AS t1 JOIN likes AS t2
SELECT *
FROM highschooler AS t1 INNER JOIN
likes AS t2
ON t1.ID=t2.ID1
JOIN PRACTICE
• Who like who?
SELECT *
FROM (
SELECT *
FROM highschooler AS t1
INNER JOIN
likes AS t2
ON t1.ID=t2.ID1
) AS t3
INNER JOIN
highschooler AS t4
ON t4.ID=t3.ID2
PRACTICE
• Find the names of all students who are friends with someone named Gabriel
• For every student who likes someone 2 or more grades younger than themselves,
return that student's name and grade, and the name and grade of the student
they like.
• For every pair of students who both like each other, return the name and grade of
both students. Include each pair only once, with the two names in alphabetical
order.
• Find all students who do not appear in the Likes table (as a student who likes or is
liked) and return their names and grades. Sort by grade, then by name within
each grade.
MORE RESOURCES
• Stanford University online learning SQL
• https://lagunita.stanford.edu/courses/DB/SQL/SelfPaced/courseware/ch-sql/seq-vid-
introduction_to_sql/
• H2 Tutorial:
• http://www.h2database.com/html/tutorial.html
• W3School SQL commands + practice:
• https://www.w3schools.com/sql/
• Practice SQL
• https://sqlzoo.net/

More Related Content

What's hot (15)

Web Service Workshop - 3 days
Web Service Workshop - 3 daysWeb Service Workshop - 3 days
Web Service Workshop - 3 days
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Relational model
Relational modelRelational model
Relational model
 
Order #188231367 (status writer assigned) role model albert e
Order #188231367 (status writer assigned) role model   albert eOrder #188231367 (status writer assigned) role model   albert e
Order #188231367 (status writer assigned) role model albert e
 
Indexes
IndexesIndexes
Indexes
 
Xml basics concepts
Xml basics conceptsXml basics concepts
Xml basics concepts
 
Database Indexes
Database IndexesDatabase Indexes
Database Indexes
 
Types of no sql databases
Types of no sql databasesTypes of no sql databases
Types of no sql databases
 
The Relational Model
The Relational ModelThe Relational Model
The Relational Model
 
Xml
XmlXml
Xml
 
Relational database- Fundamentals
Relational database- FundamentalsRelational database- Fundamentals
Relational database- Fundamentals
 
Access 02
Access 02Access 02
Access 02
 
demo2.ppt
demo2.pptdemo2.ppt
demo2.ppt
 
Database management systems 3 - Data Modelling
Database management systems 3 - Data ModellingDatabase management systems 3 - Data Modelling
Database management systems 3 - Data Modelling
 
Advance database system(part 5)
Advance database system(part 5)Advance database system(part 5)
Advance database system(part 5)
 

Similar to An hour with Database and SQL

19IS305_U2_LP4_LM4-22-23.pdf
19IS305_U2_LP4_LM4-22-23.pdf19IS305_U2_LP4_LM4-22-23.pdf
19IS305_U2_LP4_LM4-22-23.pdfGOWTHAMR721887
 
ADBMS - Joins , Sorting , Distributed Database Systems
ADBMS - Joins , Sorting , Distributed Database SystemsADBMS - Joins , Sorting , Distributed Database Systems
ADBMS - Joins , Sorting , Distributed Database SystemsJoshua Costa
 
Data_base.pptx
Data_base.pptxData_base.pptx
Data_base.pptxMohit89650
 
unit01-Activity-SQL-Query-Review-1.pptx
unit01-Activity-SQL-Query-Review-1.pptxunit01-Activity-SQL-Query-Review-1.pptx
unit01-Activity-SQL-Query-Review-1.pptxAnesElmouaddeb
 
Foundations of Data Analytics
Foundations of Data AnalyticsFoundations of Data Analytics
Foundations of Data Analyticsmrichards1
 
Tk2323 lecture 7 sql
Tk2323 lecture 7   sql Tk2323 lecture 7   sql
Tk2323 lecture 7 sql MengChun Lam
 
Data Base Management System.pdf
Data Base Management System.pdfData Base Management System.pdf
Data Base Management System.pdfTENZING LHADON
 
Querying_with_T-SQL_-_01.pptx
Querying_with_T-SQL_-_01.pptxQuerying_with_T-SQL_-_01.pptx
Querying_with_T-SQL_-_01.pptxQuyVo27
 
Apache Cassandra Developer Training Slide Deck
Apache Cassandra Developer Training Slide DeckApache Cassandra Developer Training Slide Deck
Apache Cassandra Developer Training Slide DeckDataStax Academy
 

Similar to An hour with Database and SQL (20)

19IS305_U2_LP4_LM4-22-23.pdf
19IS305_U2_LP4_LM4-22-23.pdf19IS305_U2_LP4_LM4-22-23.pdf
19IS305_U2_LP4_LM4-22-23.pdf
 
ADBMS - Joins , Sorting , Distributed Database Systems
ADBMS - Joins , Sorting , Distributed Database SystemsADBMS - Joins , Sorting , Distributed Database Systems
ADBMS - Joins , Sorting , Distributed Database Systems
 
Data_base.pptx
Data_base.pptxData_base.pptx
Data_base.pptx
 
unit01-Activity-SQL-Query-Review-1.pptx
unit01-Activity-SQL-Query-Review-1.pptxunit01-Activity-SQL-Query-Review-1.pptx
unit01-Activity-SQL-Query-Review-1.pptx
 
lecture5.ppt
lecture5.pptlecture5.ppt
lecture5.ppt
 
SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
 
Foundations of Data Analytics
Foundations of Data AnalyticsFoundations of Data Analytics
Foundations of Data Analytics
 
SQL_Part1
SQL_Part1SQL_Part1
SQL_Part1
 
Tk2323 lecture 7 sql
Tk2323 lecture 7   sql Tk2323 lecture 7   sql
Tk2323 lecture 7 sql
 
Interaction
InteractionInteraction
Interaction
 
My sql
My sqlMy sql
My sql
 
Data Base Management System.pdf
Data Base Management System.pdfData Base Management System.pdf
Data Base Management System.pdf
 
Sql
SqlSql
Sql
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
Querying_with_T-SQL_-_01.pptx
Querying_with_T-SQL_-_01.pptxQuerying_with_T-SQL_-_01.pptx
Querying_with_T-SQL_-_01.pptx
 
Apache Cassandra Developer Training Slide Deck
Apache Cassandra Developer Training Slide DeckApache Cassandra Developer Training Slide Deck
Apache Cassandra Developer Training Slide Deck
 
603s129
603s129603s129
603s129
 
MS SQL Server
MS SQL ServerMS SQL Server
MS SQL Server
 
Access 01
Access 01Access 01
Access 01
 
Normalization
NormalizationNormalization
Normalization
 

Recently uploaded

代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfJohn Sterrett
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一fhwihughh
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAbdelrhman abooda
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...ThinkInnovation
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceSapana Sha
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 

Recently uploaded (20)

代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdf
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
办理学位证纽约大学毕业证(NYU毕业证书)原版一比一
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
Predictive Analysis - Using Insight-informed Data to Determine Factors Drivin...
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts Service
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 

An hour with Database and SQL

  • 1. AN HOUR WITH DATABASE AND SQL MARIHACKS LEARNATHON IRAJ HEDAYATI
  • 2. RELATIONAL DATABASE • Entities and their relations • One-to-one relation • Many-to-Many • Many-to-one relation Mathematics Emma Emma Caleb Caleb fills fills Seat 1 Seat 2 History
  • 3. TABLE • Table or sometimes called Relation • Each entity is a table • Sometimes a relation is a table (e.g. many-to-one) • Each table is a set of Rows (also called Record or Tuple • Each Row consists a set of Columns (also called Attributes) • Key: there are different key definition but we focus only on Primary Key and Foreign Key • Schema is structure and design of database (tables and relations) ID Name Telephone Age 123456 Caleb 514222345 6 17 456789 Emma 514111987 6 16 Column Row Primary Key Unique for each row
  • 4. DESIGN SCHEMA • For each entity add a table that reflects attributes of that entity • For each entity select (or add) a unique column as primary key • Relations: • One-to-One and Many-to-One and One-to-Many: in the source table (e.g. student) add a column that holds primary key of destination table (e.g. seat). This is called Foreign Key) • Many-to-Many: add a table for this relation. It has Foreign Keys to each entity involved in this relation (e.g. Student ID and Course ID). It is like breaking it down to two One- to-Many and Many-to-One relations.
  • 6. SYSTEM REQUIREMENTS • An in-memory database: H2 • Download: http://www.h2database.com/html/download.html • H2 is JVM based, so you need install Oracle Java 7 or later • Start using H2: • http://www.h2database.com/html/tutorial.html#tutorial_starting_h2_console • Prepare schema: • https://s3-us-west-2.amazonaws.com/prod-c2g/db/Winter2013/files/social.sql
  • 7. SQL A language to query a database
  • 8. INTRO • Data Definition Language (DDL): define schema • Data Manipulation Language (DML): • INSERT • UPDATE • SELECT
  • 9. INSERT INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); INSERT INTO highschooler (id, name, grade) VALUES (1510, 'Jordan', 9); INSERT INTO highschooler VALUES (1510, 'Jordan', 9); Both are the same. Because we provided values for each column and they are in the same order.
  • 10. UPDATE UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; UPDATE highschooler SET name='Caleb' WHERE id=1468
  • 11. SELECT SELECT column1, column2, column3, … FROM table1 WHERE c1 AND c2 OR c3 • FROM: which table? • WHERE: filter rows • SELECT: what to return? • Column names • A function • STAR
  • 12. SELECT • SELECT * FROM highschooler; • SELECT name FROM highschooler WHERE grade=9; • SELECT COUNT(*) FROM highschooler WHERE grade=10; • SELECT * FROM highschooler WHERE grade>=10 AND (name LIKE 'A%' OR id>1700) • SELECT * FROM highschooler ORDER BY name; • SELECT grade, COUNT(*) FROM highschooler GROUP BY grade;
  • 13. SELECT (JOIN) • Join operation is a cartesian product (also called FULL OUTER JOIN) • INNER JOIN only return those that match SELECT * FROM highschooler AS t1 JOIN likes AS t2 SELECT * FROM highschooler AS t1 INNER JOIN likes AS t2 ON t1.ID=t2.ID1
  • 14. JOIN PRACTICE • Who like who? SELECT * FROM ( SELECT * FROM highschooler AS t1 INNER JOIN likes AS t2 ON t1.ID=t2.ID1 ) AS t3 INNER JOIN highschooler AS t4 ON t4.ID=t3.ID2
  • 15.
  • 16. PRACTICE • Find the names of all students who are friends with someone named Gabriel • For every student who likes someone 2 or more grades younger than themselves, return that student's name and grade, and the name and grade of the student they like. • For every pair of students who both like each other, return the name and grade of both students. Include each pair only once, with the two names in alphabetical order. • Find all students who do not appear in the Likes table (as a student who likes or is liked) and return their names and grades. Sort by grade, then by name within each grade.
  • 17. MORE RESOURCES • Stanford University online learning SQL • https://lagunita.stanford.edu/courses/DB/SQL/SelfPaced/courseware/ch-sql/seq-vid- introduction_to_sql/ • H2 Tutorial: • http://www.h2database.com/html/tutorial.html • W3School SQL commands + practice: • https://www.w3schools.com/sql/ • Practice SQL • https://sqlzoo.net/