Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

LAB ACTIVITY 5: Structured Query Language (SQL)

– Part 2
Duration:4 Hours

Learning Outcomes
This activity encompasses activities 5C and 5D.

At the end of this activity session, you should be able to:


1. Understand and apply DDL commands.
2. Understand and apply DML commands
3. Understand and apply SQL Advanced commands
4. Understand and apply SQL Functions

Hardware / Software: MySQL

SCENARIO

Miss Suria has been given the task of developing a database for a system of salary for the
company Millennium Cyber Sdn. Bhd. Miss Suria only has been given a month to complete
the task. Based on IT Manager of Infinity Design Solution Sdn Bhd, Miss Suria will be helped
by a practical student. Unfortunately, the student is not familiar with the concept of develop
database by using MySQL. Hence, Miss Suria need to implement workshop for students
related to the concept of SQL. These are some exercises the student will do to improve her
skills in SQL.

INSTRUCTION:
Answer the entire questions below.

Activity 5C
Activity Outcome: Able to understand and apply SQL Advanced commands

Database Name: CAR

CUSTOMER

custid custname Car_number Phone_number Register_date


P0001 David Lim WKM 1234 0139845263 12/06/14
P0002 Abu Hassan DBA 8999 0126377298 23/05/14
P0003 Low Ban Huat PFL 3434 0197894563 05/01/14
P0004 Karigalan BGN 2511 0112356897 23/05/13
P0007 Kamal Ibrahim WEP1103 0145689471 12/07/14
CAR_COMPONENT

compID compName price


KK001 ISWARA Rear Lamp 100
KK002 WIRA Front Bumper 450
KK003 ISWARA Front Mirror 500
KK004 WAJA Break Lamp 250
KK005 WIRA Front Lamp 350
CLAIM

claimID claim_date custid


CL001 02/06/14 P0002
CL002 10/07/14 P0001
CL003 24/07/13 P0004
CL004 05/08/14 P0002
CL012 07/07/14 P0003
COMPONENT_CLAIM

claimID compID quantity


CL001 KK002 1
CL001 KK005 2
CL003 KK001 2
CL012 KK003 1
CL004 KK001 2
CL002 KK003 1
CL003 KK003 1
Procedures:

Step 1 : Create new blank database name CAR. (Refer Activity 5C)
Step 2 : Create table CUSTOMER, CAR_COMPONENT, CLAIM and COMPONENT_CLAIM
Step 3 : Insert attributes of each table.
Step 4 : Insert All the data into each table.
Step 5 : Type SQL command to find customer name from CLAIM table.
SELECT custname FROM CUSTOMER
WHERE custid IN (SELECT custid FROM CAR_CLAIM);

Step 6 : Type SQL command to display component ID, component name and price where
price is between 200 and 400.
SELECT compID, compName, price FROM COMPONENT
WHERE price BETWEEN 200 AND 400;

Step 7 : Type SQL command to display car number begin with D.


SELECT custid, custname, car_number FROM CUSTOMER
WHERE car_number like 'D%';
Step 8 : Type SQL command to display claim ID and quantity where
claim number is CL001 and CL012.

SELECT claimID, quantity FROM COMPONENT_CLAIM


WHERE claimID IN ("CL001","CL012");
Activity 5D

Activity Outcome: Able to understand and apply SQL Functions.


(Refer Activity 5C)

Procedures:

Step 1 : Open the SQL Editor and type SQL command to find the minimum value for price
in the COMPONENT table

SELECT MIN(price) AS MINPRICE


FROM CAR_COMPONENT;

Step 2 : Open the SQL Editor and type SQL command to find the maximum value for
price in the COMPONENT table

SELECT MAX(price) AS MAXPRICE


FROM CAR_COMPONENT;

Step 3 : Open the SQL Editor and type SQL command to find the average value for price
in the table COMPONENT.
SELECT AVG(price) AS AVGPRICE
FROM CAR_COMPONENT;
Step 4 : Type SQL command to find average value for quantity in the table
COMPONENT_CLAIM.

SELECT AVG(quantity) AS AVGQUANTITY


FROM COMPONENT_CLAIM;

Step 5 : Open the SQL Editor and type SQL command to sum the price in the table
COMPONENT
SELECT SUM(price) AS TOTALPRICE
FROM CAR_COMPONENT;
Step 6 : Type SQL command to sum the components quantity
in the table COMPONENT_CLAIM.

SELECT SUM(quantity) AS TOTALQUANTITY


FROM COMPONENT_CLAIM

Step 7 : Open the SQL Editor and type SQL command to count the
number of customers
SELECT COUNT(custid) AS NumberOfCustomer
FROM CUSTOMER;

Step 8 : Type SQL command to count the rows in CLAIM table.


SELECT COUNT(*)
FROM CAR_CLAIM;
Step 9 : Open the SQL Editor and type SQL command to sum the quantity
component group by claim number.

SELECT claimID, SUM(quantity) AS TOTAL


FROM COMPONENT_CLAIM
GROUP BY claimID;

Step 10 : Open the SQL Editor and type SQL command to find the total
quantity group by claimID which less than 1 in

SELECT claimID, SUM(quantity) AS TOTAL


FROM COMPONENT_CLAIM
GROUP BY claimID
HAVING SUM(quantity) > 1;
COMPONENT_CLAIM table.

You might also like