You are on page 1of 7

GORDON COLLEGE

College of Computer Studies

ITC122A/L – INFORMATION MANAGEMENT 1


LABORATORY ACTIVITY NO. 7
TITLE: SQL Joins

NAME: Daryl John S. Tadeo Date: April 6 2019


Course & Block: BSIT – 1D Score: _____________

OBJECTIVE:
The objective of this lab exercise is to provide you with the ability to further practice and enhance your SQL
statements and command skills through the following:
 Retrieving data from multiple tables;
 Understanding relations between tables and;
 Using relational algebra through SQL Joins

INSTRUCTION:
 Write your answer queries in the space provided for checking.
 Include an output screenshot of the queries.
 Once done answering Lab 7, call the attention of your instructor for checking.
 Upload the file to http://bit.do/im1form7

Part I. DATABASE AND TABLE


Answer the following using SQL Queries:

Note: Login with mysql root user with the default password
Create a database with the following format:

1. Create the following table inside the create database with the following relational model:
Note: Numeric can be replaced with INTEGER(11), key means primary key, line means foreign key.

Page 1 of 7
GORDON COLLEGE
College of Computer Studies

CREATE TABLE orders(ORD_NO INT(11) NOT NULL AUTO_INCREMENT,


PURCH_AMT DECIMAL(8,2),
ORD_DATE DATE,
CUSTOMER_ID INT(5),
SALESMAN_ID(5),
PRIMARY KEY(ORD_NO));

CREATE TABLE customer(CUSTOMER_ID INT(11) NOT NULL AUTO_INCREMENT,


CUST_NAME VARCHAR(30),
CITY VARCHAR(15),
GRADE INT(3),
SALESMAN_ID INT(5)
PRIMARY KEY(CUSTOMER_ID));

ALTER TABLE orders


ADD FOREIGN KEY (CUSTOMER_ID) REFERENCES customers(CUSTOMER_ID);

2-5. In the create table, insert the following data as indicated below:
Order Table:

Page 2 of 7
GORDON COLLEGE
College of Computer Studies

Customer Table:

Page 3 of 7
GORDON COLLEGE
College of Computer Studies

//* for customer table *//

6. In Order Table, find the highest purchase amount.


SELECT MAX(PURCH_AMT)
FROM orders;

7. In Order Table, find the average purchase amount.


SELECT AVG(PURCH_AMT)

Page 4 of 7
GORDON COLLEGE
College of Computer Studies

FROM orders;

8. In Customer Table, display only the unique city of the customers.


SELECT DISTINCT CITY
FROM customers;

9. In Order Table, count the total number of unique orders.


SELECT DISTINCT COUNT(ORD_NO)
From orders;

10-11. Using Order Table and Customer Table, join the two table with their customer id and display order no,
purchase amount, customer name and their cities.
SELECT ORD_NO,PURCH_AMT,CUST_NAME,CITY
FROM orders FULL JOIN customer;

12-13. Using Order Table and Customer Table, join the two table with their customer id and display order no,
purchase amount, customer name and their cities for those orders which order purchase amount are less than
1000.
SELECT ORD_NO, PURCH_AMT,CUST_NAME,CITY
FROM orders FULL JOIN customer
WHERE PURCH_AMT < 1000;

14-15. Using Order Table and Customer Table, join the two table with their customer id and display order no,
purchase amount, customer name and the city for the customer with the name ‘Salomon Jan’.
SELECT ORD_NO, PURCH_AMT,CUST_NAME,CITY
FROM orders FULL JOIN customer
WHERE CUST_NAME = “Salomon Jan”;

16-17. Using Order Table and Customer Table, join the two table with their customer id and display order no,
purchase amount, customer name and their cities for those orders that came from Olongapo City.
SELECT ORD_NO, PURCH_AMT,CUST_NAME,CITY
FROM orders FULL JOIN customer
WHERE CITY=”Olongapo”;

Page 5 of 7
GORDON COLLEGE
College of Computer Studies

18-19. Using Order Table and Customer Table, join the two table with their customer id and display customer id,
customer name, purchase amount and the order dates for those customers that has a grade of 100 or 200 ordered
by order date ascending.
SELECT CUST_NAME,PURCH_AMT,ORD_DATE
FROM orders FULL JOIN customer
WHERE GRADE < 201
ORDER BY ORD_DATE ASC

20-21. Using Order Table and Customer Table, join the two table with their customer id and display customer id,
customer name, purchase amount and the order dates for those customers that has a name that starts with ‘M’.
SELECT CUST_NAME,PURCH_AMT,ORD_DATE
FROM orders FULL JOIN customer
WHERE CUST_NAME LIKE ‘M%’;

22-23. Using Order Table and Customer Table, join the two table with their customer id and display customer id,
customer name, purchase amount and the order dates that has the customer with the highest number of unique
orders.
SELECT MAX(ORD_NO),CUST_NAME,PURCH_AMT,ORD_DATE
FROM orders FULL JOIN customer;

24. In Order Table, update the purchase amount of the order number 70011 to 2050.5
UPDATE orders
SET PURCH_AMT = 2050.5
WHERE ORD_NO= 70011;

25. In Customer Table, update the customer name “Maria Castro” to “Maria Sy”.
UPDATE customer
SET CUST_NAME = “Maria Sy”
WHERE CUST_NAME =”Maria Castro”;

26. In Customer Table, delete the customer with the id of 3006.


DELETE FROM customer
WHERE SALESMAN_ID = 3006;

Page 6 of 7
GORDON COLLEGE
College of Computer Studies

Prepared by:

Jet Renz R. Ferrer


Instructor I
CCS Department

Page 7 of 7

You might also like