0% found this document useful (0 votes)
21 views63 pages

SQL - Student

The document provides a series of exercises and examples focused on SQL queries related to databases, particularly for managing and retrieving data about bikes, customers, and superheroes. It includes tasks for writing SQL statements to select, count, and sum data based on specific criteria. Additionally, it explains SQL concepts such as SELECT, WHERE, and ORDER BY, along with practical applications for querying data.

Uploaded by

sm0004837
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views63 pages

SQL - Student

The document provides a series of exercises and examples focused on SQL queries related to databases, particularly for managing and retrieving data about bikes, customers, and superheroes. It includes tasks for writing SQL statements to select, count, and sum data based on specific criteria. Additionally, it explains SQL concepts such as SELECT, WHERE, and ORDER BY, along with practical applications for querying data.

Uploaded by

sm0004837
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 63

9.

Databases (SQL)
Unit
Objectives
Silent Starter
1. Make yourself familiar with the example data below, then move on1a
to
the next slide.

Silen
t
Star
t
Silent Starter 1b
2. Answer the query question below. From the previous slide. Copy and
paste the yellow boxes as necessary.

Tyre Code Stock Level Width Terrain


BIKETYRES BIKETYRES BIKETYRES BIKETYRES

32 Mixed

Silen
t
Star
t
Open SQL – Student PowerPoint in Teams
Complete Slide 5 and 6 Silent Starter 2a

Write an SQL statement that will select Tyre Code, Width, Diameter for
all the Asphalt bikes.

Silen
t
Star
t
Open SQL – Student PowerPoint in Teams
Complete Slide 5 and 6 Silent Starter 2a

Write an SQL statement that will select Tyre Code, Width, Diameter for
all the Asphalt bikes.
Answer here:
SELECT Tyre Code, Width, Diameter
Silen FROM BIKETYRES
t WHERE Terrain = “Asphalt”;
Star
t
Silent Starter 2b

Write an SQL statement that will select Tyre Code, Width, Diameter for
all the mixed bikes, where stock level is less than 10.

Silen
t
Star
t
Silent Starter 2b

Write an SQL statement that will select Tyre Code, Width, Diameter for
all the mixed bikes, where stock level is less than 10.
Answer here:
SELECT Tyre Code, Width, Diameter
Silen FROM BikeTyres
t WHERE Terrain = “Asphalt” AND Stock Level < 10
Star
t
Silent Starter 3a

Write an SQL statement that will count the number of bikes within the
table.
Answer here:

Silen
t
Star
t
Silent Starter 3a

Write an SQL statement that will count the number of bikes within the
table, that have NO Tube and stock level of less than 20.

Answer here:

Silen
t
Star
t
Keywords: Databases
(SQL)
Key Word Definition
SQL Structured Query Language, which is a computer language for
storing, manipulating and retrieving data stored in a relational
database
Field
QUERY – AAcharacteristic of data
method of retrieving something
stored in astored in the
database.
databaseA–specific
SELECT The columns
query which only retrieves data.
Record
ORDER BY –SQL
Allcode,
the which
dataallows
about one
you item
to order inthe
how a database – The
data will be viewed
rows are (either ascending or descending).
a collection of fields.
SUM SQL code – a specific type of query, where a total of a field is
returned.
COUNT SQL code – a specific type of query, where a count of the number
of records is returned.
Keyw
o rds
Querying

fi l te r i n g /
gu a g e for
p u ter l a n d a ta .
o m ti n g
SQL is a c g / editing or dele
mm a r i si n
su

Teac
Expl her
anat
i on
Querying

Presentation This is a query for customer


Mode Needed
addresses.
It will filter out customers that:
1. Customers that live in
Raleigh
2. OR with a zipcode of
27513

Teac
Expl her 3. Sorted in asc. order of Last
anat
i on Name
Basic SQL

Teac
Expl her
anat
i on
Querying- SQL
After you have made a query, you
can go to <SQL View>
This will show you the SQL code
for the query.

MS Access basically makes the


SQL code for you…via the design
view.

Teac However, you will need to read


Expl her
anat
i on and write SQL in the exam
Querying- SQL

Presentation
Mode Needed

The order for SQL as follows:


1. List each field to view and / or filter on. (SELECT)
SELECT First Name, Last Name, Street Address, City, Zip Code

Teac
Expl her
anat
i on
Querying- SQL

Presentation
Mode Needed

The order for SQL as follows:


2. List each table once (there can be many tables)
(FROM)
SELECT First Name, Last Name, Street Address, City, Zip Code
FROM CUSTOMERS
Teac
Expl her
anat
i on
Querying- SQL

Presentation
Mode Needed

The order for SQL as follows:


3. List each of the criteria (separating with AND /
OR) (WHERE)
SELECT First Name, Last Name, Street Address, City, Zip Code
FROM CUSTOMERS
Teac WHERE City = ‘Raleigh’
Expl her
anat
i on
Querying- SQL

Presentation
Mode Needed

The order for SQL as follows:


4. List the ORS (OR)
SELECT First Name, Last Name, Street Address, City, Zip Code
FROM CUSTOMERS
WHERE City = ‘Raleigh’

Teac Or Zip Code = ‘27513’


Expl her
anat
i on
Querying- SQL

Presentation
Mode Needed

The order for SQL as follows:


5. State the order ascending / descending (ORDER
BY)
SELECT First Name, Last Name, Street Address, City, Zip Code
FROM CUSTOMERS
WHERE City = ‘Raleigh’

Teac Or Zip Code = ‘27513’


Expl her ORDER BY Last Name ASC;
anat
i on
Task 1: Practice Query
1. From the query below, write out the SQL statement. 1.

Answer here:

Task
Task 2: Practice
1. From the query below, write out the SQL statement. Query 2.

“Dubai”

Answer here:

Task
Task 3: Practice
1. From the query below, write out the SQL statement. Query 3.

Ascending

“Dubai”
“Abu Dhabi”

Answer here:

Task
Worked Example
From the query below, write out the SQL statement.

1. Write a query to get Product name and quantity/unit. Table Name is


Products
Answer here:

Task
Task 5: Practice Query
2.
From the query below, write out the SQL statement.

1. Write a query to get Product list (id, name, unit price) where products cost between $15
and $25 inclusive. Table Name = Products

Answer here:
Select Product Id, Product name, Unit price (£)
From products
Where unit price >= £15 and <= 25;
Task
Task 6: Practice
1. From the sample data below, writeQuery 3.
the query in SQL.

Write a query to display the names (first_name, last_name) where the salary is greater than
15000
Table Name=‘Employees’.
Answer here:
Select first_name , last_name
From employees
Where salary is > 15000

Task
Task 7: Practice Query
4.
1. From the sample data below, write the query in SQL.

Write a query to display the names (first_name, last_name) where the Job ID = ’IT_PROG’ or
‘AD_VP’ AND Salary is greater than 8000. Table Name = ‘Employees’.

Answer here:
Select first_name , last_name
From employees
Where Job_Id = ‘IT_PROG’ or ‘AD_VP’
And salary is > 8000;
Task
Task 8: Practice Query
5.
1. From the sample data below, write the query in SQL.

Write a query to display the names (first_name, last_name) where the manager ID=100
order the query by last name is ascending order. Table Name = ‘Employees’.

Answer here:
Select first_name , last_name
From employees
Where manager Id = 100
Order by last_name;

Task
Task 9: Practice Query
6.
1. From the sample data below, write the query in SQL.

Write a query to find the name Street Address, Postal Code, Country Name. Tables Names =
Location and Country

Answer here:
Select street_adress , postal_code , country_name
From location and country

Task
Task 10: Practice
1. From the sample data below, writeQuery 7.
the query in SQL.

Create a query to show the street address, postal code, state_province and country name for
all country IDs which are ‘US’ or ‘JP’. Table Name = Location and Country.

Answer here:
Select street_adress , postal_code , state_province ,
country_name
FROM LOCATION and COUNTRY
WHERE country_ID = ‘US’ or ‘JP’
Task
Summary Query

n a m e ) This will count the


( c o um n_
l number of records
C O UN T that meet a
T
SELEC a me condition.
b l e _ n
t a
FROM i on
on d i t
R E c
WHE
SELECT SU
M(stock_l
This will total a field, evel)
where the records FROM tabl
e_name
meet a specific
criteria. WHERE con
Teac ditiion
Expl her
anat
i on
Summary Query

Presentation
Mode Needed

he To count the num


For counting t ber of TEAMS:
ords I
number of rec SELECT COUNT(TE
AM)
ey field,
would use a k
always FROM LeagueTable
Teac or a field that ;
Expl her h a s d a ta .
anat
i on
Summary Query

Presentation
Mode Needed

e numb er of T EAMS:
To count th
SELECT COUNT(TEAM)

Teac FROM LeagueTable;


Expl her
anat
i on
Summary Query

Presentation
Mode Needed

To count the num


ber of TEAMS who
he
For counting t have got 60 points
ore more:
ords I
number of rec SELECT COUNT(TEA
u se a k ey fi eld, M)
would
always
or a field that
FROM LeagueTable
Teac h a s d a ta . WHERE Pts>=60;
Expl her
anat
i on
Summary Query

Presentation
Mode Needed

co u nt th e n u m ber of TEAMS who


To
av e g o t 6 0 po in ts ore more:
h
SELECT COUNT(TEAM)

Teac FROM LeagueTable


Expl her WHERE Pts>=60;
anat
i on
Summary Query

Presentation
Mode Needed

To count the num


he ber of TEAMS wh
For counting t 60 points or more o h a v e g ot
a n d w o n 2 0 ga m e
ords I
number of rec
s or more.
SELECT COUNT(TEA
u se a k ey fi eld, M)
would
always FROM LeagueTable
or a field that
h a s d a ta .
WHERE Pts>=60 AN
Teac D Won>=20;
Expl her
anat
i on
Summary Query

Presentation
c
Mode Needed

A MS w ho have got
TE
cou nt t he number of ames or
To d w on 2 0 g
0 p oin ts o re more an
6
more.
T(TEAM)
SELECT COUN
Teac FROM League
Table
Expl her ND Won>=20;
anat WHERE P ts > =6 0 A
i on
Summary Query

Presentation
Mode Needed

To add how many


points where
y ou
For summing won from every t
eam:
he na m e
need to add t
u wish SELECT SUM(Pts)
of the field yo
t o t o ta l
Teac FROM LeagueTable;
Expl her
anat
i on
Summary Query

Presentation
Mode Needed

a n y p oin t s were
To add how m
om e ve r y te a m:
won fr
s)
SELECT SUM(Pt
Teac ;
Expl her FROM LeagueTable
anat
i on
Summary Query

Presentation
Mode Needed

To add how many


points where won
y ou from every team:
For summing
he na m e
need to add t SELECT SUM(GF)
u wish
of the field yo FROM LeagueTable;
t o t o ta l
Teac WHERE WON>=20;
Expl her
anat
i on
Summary Query

Presentation
Mode Needed

ow m a ny points where won


To add h
from every team:
SELECT SUM(GF)
FROM LeagueTable;
Teac
Expl her WHERE WON>=20;
anat
i on
Task 12.1: Summary Query
1.
1. From the sample data below, write the query in SQL.

Create a query to count how many superheroes there are. TABLE NAME = SuperHeroes.

Answer here:
SELECT count(superhero)
FROM Superheroes;

Task
Task 12.2: Summary
1. From the sample data below, write Query 2.
the query in SQL.

Create a query to count how many males superheroes there are, with Super Strength as their
power 1. TABLE NAME = SuperHeroes.

Answer here:
SELECT COUNT(SUPERHERO)
FROM SuperHeroes
WHERE gender = ‘male’
AND power 1 is = ‘super strength’;
Task
Task 13: Summary Query
3.
1. From the sample data below, write the query in SQL.

Create a query to calculate the total weight of all female superheroes there are. TABLE
NAME = SuperHeroes.

Answer here:

Task
Task 14: Summary Query
4.
1. From the sample data below, write the query in SQL.

Create a query to calculate the total height of all female superheroes there are, with black
hair. TABLE NAME = SuperHeroes.

Answer here:

Task
Task 14: Progress
Check

Create a query to calculate the total height of all female superheroes there are, with black
hair. TABLE NAME = SuperHeroes.

SELECT SUM(HEIGHT)
FROM SuperHeroes
WHERE Gender = ‘Female’ AND Hair=‘Black’;
Feed
ba ck
Task 15: Orders 1

Write a SQL statement to find the total purchase amount of all orders. Table
Name = Orders

Answer here:

Task
Task 16: Orders 3

Write a SQL statement to find to count the number of customers. Table Name
= Customers

Answer here:

Task
Task 17: Orders 3

Write a SQL statement to find to count the number of customers based in New
York.
Table Name = Customers
Answer here:

Task
Summary Query – Group
By
Presentation
Mode Needed

t a tistic s th at a re
m a y w an t to fi nd s
Sometimes you
grouped. e d in e ach
r h eroes a re b a s
ma ny sup e
Lets see how
city.
Teac
Expl her
anat
i on
Summary Query – Group
By
Presentation
Mode Needed

rhe ro e s you w o uld do:


t t h e nu m b e r o f supe
To coun
C OU NT ( Su p e r hero)
SELECT
Count o
FROM Superhe r o e s f Super
h ero
w ould give yo u…
This
Teac
Expl her
anat
i on
Summary Query – Group
By
Presentation
Mode Needed

tego ry in th at field.
l add u p each c a
UP B Y it wil
By adding GRO
T CO UN T( S u pe rhero)
SELEC
oes
FROM Superher
.
GROUPBY Lives
Teac
Expl her
anat
i on
Task 18: Orders 2

Write a SQL statement to find the total purchase amount of all orders for
categorised by customer ID. Table Name = Orders

Answer here:

Task
Task 19: Orders 2

Write a SQL statement to find the total purchase amount of all orders for
each salesman Table Name = Orders

Answer here:

Task
Plenary Task 1

Fix the errors in the SQL statement from the table above.
This should show all fields, expect Subject Taken, for all students the that
are doing computer science

SELECT Student, Form, Subject Taken, Subject Level, DOB


From Students
Plen WHERE Subject=“ICT”
a ry
Plenary Task 2

Fix the errors in the SQL statement from the table above.
This should show COUNT the number of students taking MATHS.

SELECT COUNT()
FROM Student
WHERE Subject Level = “A”
Plen
a ry
Plenary Task 3
Assess yourself on your logic gates skills so far…

I know how to…. Can I do it? I know how to Can I do it?


complete a truth
table for…
Write a basic SQL Create a complex query (>=2
statement to show all criteria)
records
Write a basic SQL Create a basic summary SUM
statemen, with 1 criteria. query
Write a basic SQL Create a basic summary
statemen, with 2+ COUNT query
criteria.
Write a basic COUNT Create a complex summary
query. COUNT query with multiple
criteria.
Write a COUNT query Create a complex summary
Plen with 1 criteria. SUM query with multiple
a ry criteria.
Task: Past Paper 1
The design for a nurses databases is as follows:

NURSE (NurseID, FirstName, FamilyName, Area, Specialism)

Write an SQL query to display the Nurse ID and family name for all Area B
nurses with a
specialism of 'THEATRE'.

Answer here:

Task
Task: Past Paper 2
The design for a movies databases is as follows:

MOVIE (MovieID, Title, Length, Rating)


SCREEN (ScreenNumber, NumberSeats)
MOVIESCHEDULE (Schedule ID, MovieID, ScreenNumber, Time)

Write an SQL script to display the title and rating of all movies scheduled to
play on screen number 3.

Answer here:

Task
Task: Past Paper 3
The design for a movies databases is as follows:

USER (UserName, FirstName, SecondName, DateOfBirth)


PHOTO (PhotoID, UserName, Comment, UploadDate)
TEXTPOST(PostID, UserName, DateOfPost, TheText)

Write an SQL script to display the username, firstname, secondname, for all
uploads dated 25/12/2020

Answer here:

Task
Task: Past Paper 4
The design for a movies databases is as follows:

LOGIN (LoginID, PlayerID, Date)


PURCHASE (PurchaseD, PlayerID, PurchaseDate, Cost)
PLAYER (PlayerID, PlayerName, SkillLevel)

Write an SQL to display the playerID, Purchase Dates and Costs for all orders
over £30, where skill level is 10 or more.

Answer here:

Task
Task: Past Paper 5
The design for a movies databases is as follows:

PATIENT (PatientID, PatientName, Address, Gender)


DOCTOR (DoctorID, DoctorName, Gender, Qualification)
APPOINTMENT (AppointmentDate, AppointmentTime, DoctorID,
PatientID)

Write an SQL to display the patient’s name, doctor’s name for all appointments
on 01/03/2020

Answer here:

Task
Task: Past Paper 6
The design for a movies databases is as follows:

STAFF (StaffID, StaffName, Department)


CLIENT (ClientName, Address, Town)
VISIT (ClientName, VisitDate)
INTERVIEW (ClientName, VisitDate, StaffID, SpecialistFocus,
InterviewText)

Write an SQL script to display the Staff ID of each member of staff who
performed an interview when they visited New Age Toys on 13/10/2016.

Answer here:

Task

You might also like