SQL Susuban

advertisement
Susuban, Ann Marie G.
TE10
SQL Bring Home Quiz
DATABASE NAME:
BRINGHOME
TABLE NAME: PERSONS
PersonId
LastName
1 Hansen
FirstName
Ola
Address
Timoteivn 10
City
Sandnes
Gender
M
Borgvn 23
Sandnes
M
Stavanger
F
Stavanger
New York
M
F
2
Svendson
Tove
3
Pettersen
Kari
4
5
Anderson
Samson
Kim
Viel
Boulevard
Walk St
TABLE NAME: ORDERS
OrderID
1
OrderNo
77895
PersonId
3
OrderPrice
100
OrderDate
4-17-89
2
44678
3
500
5-18-90
3
22456
1
2000
10-6-91
4
24562
1
600
6-3-89
5
34764
5
300
9-5-92
Instructions:
1. Create two tables namely Persons and
Orders and input the data seen
above. Copy the SQL codes that were
used to create the database with
relationship, tables, and records.
Write as PART I. See to it that proper
format in every field is observed.
2. For the following questions, run the
queries and copy the SQL statements
in a word file. Save as
SQL_Familyname and submit to
Google Drive before the deadline.
Part I
1. CREATE DATABASE bringhome;
CREATE TABLE persons(
person_id INT AUTO_INCREMENT PRIMARY KEY,
lastname VARCHAR(100),
firstname VARCHAR(100),
address VARCHAR(100),
city VARCHAR(100),
gender VARCHAR(1)
);
CREATE TABLE orders(
order_id INT AUTO_INCREMENT PRIMARY KEY,
order_no INT,
person_id INT,
order_price DOUBLE,
order_date DATE,
FOREIGN KEY(person_id) REFERENCES persons (person_id)
);
INSERT INTO persons (person_id, lastname, firstname, address, city, gender)
VALUES (1, 'Hansen', 'Ola', 'Timoteivn 10', 'Sandnes', 'M'),
(2, 'Svendson', 'Tove', 'Borgvn 23', 'Sandnes', 'M');
INSERT INTO persons (person_id, lastname, firstname, city, gender)
VALUES (3, 'Pettersen', 'Kari', 'Stavanger', 'F');
INSERT INTO persons (person_id, lastname, firstname,address, city, gender)
VALUES (4, 'Anderson', 'Kim', 'Boulevard', 'Stavanger', 'M'),
(5, 'Samson', 'Viel', 'Walk St', 'New York', 'F');
INSERT INTO orders (order_id, order_no, person_id,order_price, order_date)
VALUES
(1, 77895, 3, 100, '1989-04-17'),
(2, 44678, 3, 500, '1990-05-18'),
(3, 22456, 1, 2000, '1991-10-06'),
(4, 24562, 1, 600, '1989-06-03'),
(5, 34764, 5, 300, '1992-09-05');
PART II Questions: Using (SQL)
Using simple statements and one table only. (SELECT, DISTINCT, IS NULL, IS NOT NULL, WHERE, LIKE, BETWEEN, FROM, ORDER BY,AS)
1. List all the female person
SELECT * FROM `persons` WHERE gender = 'F';
2. List all the unique cities
SELECT DISTINCT city FROM `persons`;
3. List all the male persons who lives ‘Sandnes’
SELECT * FROM `persons` WHERE gender = 'M' AND city = 'Sandnes';
4. List the persons who’s name starts with ‘k’
SELECT firstname "Name" FROM persons WHERE firstname LIKE 'K%';
5. List the person who has an ‘e’ in their lastname and lives in ‘Stavanger’.
SELECT lastname "Name" FROM persons WHERE lastname LIKE '%e%' AND city =
'Stavanger';
6. List the customer who was born after 1978.
BONUS
7. List the customer who has no address.
SELECT * FROM `persons` WHERE address IS NULL;
8. List the customer who has a city.
SELECT * FROM `persons` WHERE city IS NOT NULL;
9. List the persons id, last name, and first name only of Person’s table
SELECT person_id, firstname, lastname "Name" FROM persons;
10. List the order no of orders whose order price is greater than 500
SELECT order_no FROM `orders` WHERE order_price > 500;
11. List the all the person in descending order of last name
SELECT * FROM `persons` ORDER BY lastname DESC;
12. List the order from year 1991 to 1992.
SELECT * FROM orders WHERE order_date LIKE '1991%' OR order_date LIKE '1992%';
13. List the order no and order price in Orders table.
SELECT order_no, order_price FROM orders;
14. Create a TaxAmount field in Orders that is calculated as 10% of OrderPrice.
ALTER TABLE orders ADD tax_amount INT;
UPDATE `orders` SET tax_amount = order_price * .10;
15. List the order who is ordered in June 1989.
SELECT * FROM orders WHERE order_date LIKE '1989-06%';
16. List all the person’s data and their orders.
SELECT * FROM persons LEFT OUTER JOIN orders
USING (person_id);
17. List the persons’s id no of customer who’s order amount is lesser than 1000.
SELECT * FROM persons LEFT OUTER JOIN orders
USING (person_id)
WHERE order_price < 1000;
18. List the person’s id no order date is from 1991 above whose order number starts at 2.
SELECT * FROM orders LEFT OUTER JOIN persons
USING (person_id)
WHERE YEAR(order_date) >= 1991
AND order_no LIKE '2%';
19. List the person who’s order no. has a 7 anywhere in its order number.
SELECT * FROM persons LEFT OUTER JOIN orders
USING (person_id)
WHERE order_no LIKE '%7%';
20. Create a TaxAmount field in Orders that is calculated as 10% of OrderPrice and the total payable computed by adding the tax
and the original order price.
ALTER TABLE orders ADD total_payable INT;
UPDATE `orders` SET total_payable = tax_amount + order_price;
Using (DELETE, INSERT INTO, UPDATE)
1. Add yourself as one of the person in Person’s table.
INSERT INTO persons(person_id, lastname, firstname, address, city, gender)
VALUES (6, 'Susuban', 'Ann', 'Camp Phillips', 'Manolo Fortich','F')
2. Add your order in Order’s table with your person id you use in no. 1
INSERT INTO orders( order_id, order_no, person_id, order_price, order_date,
tax_amount, total_payable)
VALUES (6, 76430, 6, 3500,'2019-010-15', 350, 3850);
3. Edit the gender of ‘Anderson Kim’ into female and change her name into ‘Kimmy’
UPDATE persons SET gender = 'F', firstname = 'Kimmy'
WHERE person_id = 4;
4. Delete all the person who has no address.
ALTER TABLE orders
ADD CONSTRAINT person_id
FOREIGN KEY (person_id)
REFERENCES persons(person_id)
ON DELETE CASCADE ON;
DELETE FROM `persons` WHERE address IS NULL;
Delete the order of ‘Samson Viel’.
DELETE FROM orders WHERE person_id IN
(SELECT person_id FROM persons WHERE lastname = 'Samson' AND firstname = 'Viel')
After properly executing the above statements, screenshot the results of the following statements.
1. SELECT * FROM PERSONS;
5.
2.
SELECT * FROM ORDERS;
Download