0% found this document useful (0 votes)
14 views5 pages

G8 - Managing Data - L03 - Worksheet 7 - ict

The document provides an overview of SQL (Structured Query Language) for querying relational databases, including examples of queries for single and multiple tables with various criteria. It includes a worksheet with a chess club database and questions related to SQL queries. The document aims to help users understand how to construct SQL queries using provided data tables.

Uploaded by

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

G8 - Managing Data - L03 - Worksheet 7 - ict

The document provides an overview of SQL (Structured Query Language) for querying relational databases, including examples of queries for single and multiple tables with various criteria. It includes a worksheet with a chess club database and questions related to SQL queries. The document aims to help users understand how to construct SQL queries using provided data tables.

Uploaded by

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

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]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy