Name: ___________________________________________________________________ Class:___________
Worksheet 7 – Querying databases using SQL
Summary of SQL
SQL (Structured Query Language) is a specialised language for accessing data in relational databases.
SQL to query a single data table with one criteria
SELECT <table>.<field heading>
FROM <table>
WHERE <table>.<field heading> <comparison operator> <value> ;
SQL to query two linked data tables with one criteria
SELECT <table>.<field heading>
FROM <table> LEFT JOIN <table>
ON <table>.<field heading> = <table>.<field heading>
WHERE <table>.<field heading> <comparison operator> <value> ;
SQL to query two linked data tables with two criteria
Just like in programming, the condition can have multiple criteria. To do this, we use AND or OR:
SELECT <table>.<field heading>
FROM <table> LEFT JOIN <table>
ON <table>.<field heading> = <table>.<field heading>
WHERE <table>.<field heading> <comparison operator> <value>
AND/OR
<table>.<field heading> <comparison operator> <value> ;
(see next page for questions)
Name: ___________________________________________________________________ Class:___________
Questions
1) The school chess club has the following database:
Tablename: Members
PlayerID Name Gender Age JoinDate
1 Nguyen Thi Hanh F 14 2024-08-23
2 Tran Van Nam M 15 2023-12-02
3 Le Minh Phuong F 13 2024-08-25
4 Pham Hoang Anh M 13 2024-09-01
5 Vo Thi Thu F 12 2024-01-08
6 Dang Quang Huy M 14 2024-04-11
Tablename: ChessGames
GameID PlayerID WinOrLoss WhiteOrBlack GameLength
1 1 Win White 27
2 6 Win Black 37
3 3 Loss White 40
4 2 Win White 30
5 3 Loss Black 36
6 6 Win White 29
(a) Look at this SQL query:
SELECT Members.Name, ChessGames.WinOrLoss
FROM ChessGames LEFT JOIN Members
ON ChessGames.PlayerID = Members.PlayerID
WHERE ChessGames .WinOrLoss = 'Win';
State the criteria used in the SQL query.
........................................................................................................................................................... [1]
(b) Look at this SQL query:
SELECT Members.Name, ChessGames.WinOrLoss
FROM ChessGames LEFT JOIN Members
ON ChessGames.PlayerID = Members.PlayerID
WHERE ChessGames .WinOrLoss = 'Win';
Fill in the table below, showing what is returned by the SQL query. Some columns or rows may be unused.
[2]
Name: ___________________________________________________________________ Class:___________
(For your convenience, the database is repeated below)
Tablename: Members
PlayerID Name Gender Age JoinDate
1 Nguyen Thi Hanh F 14 2024-08-23
2 Tran Van Nam M 15 2023-12-02
3 Le Minh Phuong F 13 2024-08-25
4 Pham Hoang Anh M 13 2024-09-01
5 Vo Thi Thu F 12 2024-01-08
6 Dang Quang Huy M 14 2024-04-11
Tablename: ChessGames
GameID PlayerID WinOrLoss WhiteOrBlack GameLength
1 1 Win White 27
2 6 Win Black 37
3 3 Loss White 40
4 2 Win White 30
5 3 Loss Black 36
6 6 Win White 29
(c) Look at this SQL query:
SELECT Members.PlayerID, Members.Name, ChessGames.GameLength, ChessGames.WinOrLoss
FROM ChessGames LEFT JOIN Members
ON ChessGames.PlayerID = Members.PlayerID
WHERE ChessGames.GameLength > 30 ;
Fill in the table below, showing what is returned by the SQL query. Some columns or rows may be unused.
[2]
(d) Use your answer from part (b) to answer the following question:
How many games lasted longer than 30 minutes? ........................ [1]
(e) Use the phrases below to write an SQL query that:
● returns the player's name (from the Members table) and the Game ID (from the ChessGames table);
● only returns the records where the game was a 'Loss'.
SELECT .........................................................................................................
FROM ChessGames LEFT JOIN Members
ON ............................................ = ............................................
WHERE .............................................................................; [3]
Name: ___________________________________________________________________ Class:___________
(f) Fill in the blanks below to write an SQL query that:
● returns whether a game was a win or loss, the player's name and the player's gender;
● only returns the records where the player is 13 or the player is 15.
SELECT .........................................................................................................
FROM ........................................ LEFT JOIN ..............................................
ON ............................................ = ............................................
WHERE .............................................................................
....................
............................................................................. ;
[6]
(g) Fill in the blanks below to write an SQL query that:
● returns the player's ID, the player's name and whether the game was a win or loss;
● only returns the records where the player is a girl AND the game was less than or equal to 35 minutes
long.
SELECT .........................................................................................................
FROM ........................................ LEFT JOIN ..............................................
ON ............................................ = ............................................
WHERE .............................................................................
....................
............................................................................. ;
[6]
Name: ___________________________________________________________________ Class:___________
2) Look at the data tables below. They are part of a shop's database.
Tablename: Products
Tablename: Categories
(a) Fill in the blanks below to write an SQL query that:
● returns the category ID, category name, product name and price;
● only returns the records where the category ID equals 8 and the price is greater than 20.
SELECT .........................................................................................................
FROM Products LEFT JOIN ..............................................
ON ............................................ = ............................................
WHERE .............................................................................
.............................
............................................................................. ;
[6]
(b) Check if your SQL works!
On your device, go here: https://www.w3schools.com/sql/trysql.asp?filename=trysql_editor
Copy-paste your SQL into the box:
Click on:
Write how many records were returned: ....................... [1]