Uploaded by Ahnafxyz

Pws on SQL

advertisement
Revision
n Worksheet on Database (SQL)
Chapter – 9
Name: ……………………………………….…………………,
……………………………………
Sec:……………….…………,
…………, Roll:………
Roll:………………….
1. A database table,, MARKS, was set up to record the test results for a class of students.
Part of the database table is shown below.
1
A database, MARKS, was set up to record the test results for a class of students. Part of the
database is shown below.
(c) (i) To selects the students name from the file ,class ID 0013 from the file
The SQL scripts is
SELECT Fields name
SELECT StudentName
FROM table name
FROM MARKS
WHERE Field name = value;
WHERE ClassID =’0013’;
If it is need to display in alphabetical order of student name, An ORDER BY statement
takes the form:
Re-write the SQL scripts:
SELECT Fields name
…………………………………………………
…………………………………………………
…………………………………………………
…………………………………………………
FROM Table name
WHERE Field name =’Value’
ORDER BY Field(s) name ;
(ii) To selects all students’ name with more than 50 marks and less than 60 marks in
History
Complete the incomplete following SQL scripts.
SELECT …………………………………
FROM MARKS
WHERE History>50 ………………………… History<60;
(iii) To selects the of the name of students & class id those who got lowest marks in math
from the file
Write SQL scripts:
…………………….………………………………………………………………………………………
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
2
(iv) To select and show the student names only of all students with less than 40 marks in
both Maths and English.
Complete the incomplete following SQL scripts.
SELECT StudentName
FROM MARKS
WHERE Math<40 AND English <40……………………;
(v) To selects all student names with more than 60 marks in History and greater than or
equal to 60 marks in Geography.
Write SQL scripts:
…………………….………………………………………………………………………………………
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
(vi) To select only student names with the marks between 60 to 90 in History in alphabetical
order of student name.
SELECT Fields name,….,
FROM table name
WHERE Field’s name BETWEEN 1st value AND 2nd Value
ORDER BY Fields name;
Write SQL scripts:
…………………….………………………………………………………………………………………
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
(vii) To select only student name with the marks are equal to 60, 70 or 90 in Science
SELECT Fields name,….,
FROM table name
WHERE Field’s name IN(value1,value2,value3,…);
Write an SQL script:
…………………….………………………………………………………………………………………
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
3
(viii) The following query used to count all of the students’ exam marks in Science subject. Complete
SQL script.
SELECT Function (Field name)
FROM table name
WHERE field’s name=value;
SELECT ………………………
FROM Marks;
Function: COUNT(fieldname), SUM(fieldname)
(x) Write an SQL statement used to total all of the students’ exam marks in Math subject
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
…………………………………………………………………………………………………………[3]
(xi) Write an SQL script to count how many students of in the file.
………………………….…………………………………………………………………………………
……………………………………………………………………………………………………………..
…………………………………………………………………………………………………………[3]
(d)(i) Write an SQL statement to count how many students who got marks in maths between 70 to 90.
………………………….…………………………………………………………………………………
……………………………………………………………………………………………………………..
…………………………………………………………………………………………………………[3]
(ii) Write an SQL scripts to select student name those who did not get 88 in English.
………………………….…………………………………………………………………………………
……………………………………………………………………………………………………………..
…………………………………………………………………………………………………………[3]
Example, the use of IN and BETWEEN :

Find the Fname, and Lname of the Employees who have a Salary equal to 30000,
40000, or 25000.
SQL statement:
SELECT Name
FROM Emp
WHERE Salary IN (30000, 40000, 25000);

List all the Employee’s Names who is having salary between 30000 and 45000.
SQL statement:
SELECT Name
FROM Emp
WHERE Salary
BETWEEN 30000 AND 45000;
4
(i) Select the student name those who are got marks within 40 to 70 in maths.
Write the SQL statement.
………………………….…………………………………………………………………………………
……………………………………………………………………………………………………………..
………………………………………………………………………………………………………..…[3]
(ii) Find the student name and class id those who are got marks equal to 90, 65 or 50 in science.
Write the SQL statement.
………………………….…………………………………………………………………………………
……………………………………………………………………………………………………………..
………………………………………………………………………………………………………..…[3]
2 (a) The database design has one tables to store the classes that students attend.
STUDENT(StudentID, FirstName, LastName, Year, TutorGroup)
Table name
STUDENT
StudentID
001AT
003JL
011HJ
012Kl
FirstName
Ahmed
Jane
Heather
Jane
Primary key
Non key attributes/fields
LastName
Tan
Li
Jones
Rose
Year
2020
2021
2022
2022
TutorGroup
11A
10B
11B
10C
(i) Write an SQL script to display the StudentID and FirstName of all students who are
in the tutor group 10B. Display the list in alphabetical order of LastName.
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
(ii) Write an SQL script to display the StudentID and Year of all students who are
in the tutor group 11A,10B or 10C. Display the list in alphabetical order of First name
followed by LastName. Show the result/output
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
5
(ii) Write an SQL script to display the LastName of all students who attend the class whose
Year is 2022.
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
3 The part of database Product, the keep the record of stock of products.
ProdID
123
124
125
Name
Red leaf
Sigmaflo
Sigmaflo
Color
R
B
B
Qty
10
15
15
(a) Write an SQL script to count how many products are in the product file.
(b) Write an SQL script to find the total (sum) quantities of stock in the product file.
(c) Write an SQL script to find the total (sum) quantities of product name is Sigmaflo in the
product table.
Sample answers and output:
(i)
SELECT Count(ProdID)
FROM Product;
Output : 3
(ii) SELECT SUM(Qty)
FROM Product;
Output : 40
(iii) SELECT SUM(Qty)
FROM Product
WHERE Name=’Sigmaflo’;
Output : 30
4 The database table, BookList
CatNo
Title
Fiction
Author
PaperBack
Price
StockLevel
BK01
BK02
BK03
BK04
BK05
BK06
BK07
BK08
BK09
The Princes’ Story
The Princesses’ Story
Computer Science
The Modern World
The Ancient World
Computer Science
AS Computer Science
Tom and Jarry
A brief history of time
Yes
Yes
No
No
Yes
Yes
No
Yes
Yes
B Penn
B Penn
Way Yu
P Patel
P Patel
R Dale
Helen W
Dave W
Hawking S
Yes
Yes
Yes
Yes
No
No
No
Yes
No
4.5
4.5
19.99
25.00
25.00
27.35
100.99
10.50
350.00
500
0
20
0
5
8
0
0
350
6
(a) State the number of records in this part of the database table.
.........................................................................................................................
(b) Complete the table to identify the most appropriate data type for each field based on the
data shown in the table BookList
(c) Write the output from this structured query language (SQL) statement.
SELECT CatNo, Title, Author
FROM BookList
WHERE StockLevel = 0;
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
(d) Complete this SQL statement to display all the titles by the author B Penn.
SELECT .....................................................................................................................................
FROM .........................................................................................................................................
WHERE .....................................................................................................................................;
(e) (i) Write an SQL statement to display title and author name which are non-fiction in order by title in
ascending order.
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
(ii) Write an SQL statement to display title and author name both are fiction and paperback order by
author name.
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
7
(iii) Write an SQL statement to select CatNo and paperback fiction type book that stock level range
within 10 to 100 (inclusive)
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
(iv) Write an SQL statement to count the number of paperback book.
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
5
The database table, Results
SName
Subject
John
Math
John
Phy
John
Chm
Mark
CS
Mark
Phy
Mark
Math
Level
O
O
O
AS
A
A
Grade
A
B
D
B
C
B
Pass
YES
YES
No
YES
YES
YES
(a) Write an SQL statement to display all the subject and grade of john.
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
(b) Write an SQL statement to select subject for those students who were passed in all subject of
student’s name in ascending order.
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
(c) To display the name those were not passed in chemistry. Write an SQL scripts
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
8
(d) (i) Count how many students were passed in mathematics. Write an SQL scripts
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
(ii) Count how many students were achieved A grade in O Level in each subject.
(you have to use LIKE operator and Group BY operator). Write an SQL scripts
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
……………………………………………………………………………………………………………..
Example of operator IN and BETWEEN
Find the Fname, and Lname of the Employees who have a Salary equal to 30000, 40000, or
25000.
SQL statement:
SELECT Name
FROM Emp
WHERE Salary IN(30000, 40000, 25000);
List all the Employee’s Names who is having salary between 30000 and 45000.
SQL statement:
SELECT Name
FROM Emp
WHERE Salary
BETWEEN 30000 AND 45000;
001AT 2020
003JL 2021
012KL 2022
Ahmed Tan
Jane Li
Jane Rose
9
Download