0% found this document useful (0 votes)
1 views25 pages

Asg2 Sqljoin

The document is an assignment on SQL JOINs submitted by Pratik Raj Bista at Saint Xavier's College, detailing the creation and manipulation of employee and customer tables. It includes various SQL commands for creating, modifying, and querying tables related to employees and customers, as well as their orders. The conclusion reflects on the learning experience regarding SQL joins and data manipulation techniques.

Uploaded by

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

Asg2 Sqljoin

The document is an assignment on SQL JOINs submitted by Pratik Raj Bista at Saint Xavier's College, detailing the creation and manipulation of employee and customer tables. It includes various SQL commands for creating, modifying, and querying tables related to employees and customers, as well as their orders. The conclusion reflects on the learning experience regarding SQL joins and data manipulation techniques.

Uploaded by

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

Saint Xavier’s College

MAITIGHAR, KATHMANDU, NEPAL


Phone: 01-5321365, 01-5344636
Email: [email protected]

ASSIGNMENT NUMBER: 2
“ SQL JOIN ”

Submitted By Submitted To Signature


Name: Pratik Raj Bista Department of
Roll No:936 Computer(+2)
Class:12 St. Xavier’s College
Section: I

Submission Date: 13th July, 2025

Table of Content

i
Contents
Table of Content..........................................................................................................................ii
PART I: Employees and their departments.....................................................................................1
1) Create a table named Employee with necessary constraints for each attribute.................1
2) Add a new column email to the Employee table..............................................................1
3) Modify the salary column to allow up to 12 digits...........................................................2
4) Rename the department column to dept_name.................................................................2
5) Rename the table Employee to staff table.........................................................................2
6) Delete the email column from the Employee table...........................................................2
7) Delete all rows from the staff table...................................................................................3
8) Drop table staff..................................................................................................................3
PART II: Customers and their orders..............................................................................................4
A. Create a table "customers" with fields (customer_id, customer_name) and fill three data
in the table.4
B. Create another table "orders" with fields (order_id, order_date, customer_id, amount) and
fill following four data:................................................................................................................5
(101, 2023-01-10, 1, 100)............................................................................................................5
(102, 2023-01-15, 2, 200)............................................................................................................5
(103, 2023-01-20, 1, 150)............................................................................................................5
(104, 2023-01-25, 4, 300)............................................................................................................5
1) Retrieve all customers who have placed an order amounting to more than 150 and the
order date is in the year 2023.......................................................................................................6
2) Retrieve all customers who have placed an order amounting to more than 150 or the
order date is in the year 2023.......................................................................................................6
3) Retrieve all orders where the order amount is not equal to 150.......................................7
4) Calculate the average order amount for all orders placed in the year 2023......................7
5) Find the minimum order amount from the orders table....................................................8
6) Find the maximum order amount from the orders table...................................................8
7) Round the average order amount to 2 decimal places......................................................9
8) Retrieve all distinct order dates from the orders table......................................................9
9) Calculate the total order amount for all orders placed by customer with ID 1...............10
10) Retrieve the length of the customer names from the customers table............................10
11) Convert all customer names to uppercase.......................................................................11

ii
12) Convert all customer names to lowercase.......................................................................11
13) Concatenate the customer name and their customer ID..................................................11
14) Reverse the customer names...........................................................................................12
15) Repeat the customer names three times..........................................................................13
16) Retrieve the first three characters of each customer name..............................................13
17) Retrieve the last three characters of each customer name...............................................14
18) Retrieve all customers whose names start with 'J'..........................................................14
19) Retrieve all customers whose names have 'o' as the second character............................14
20) Retrieve all orders ordered by order amount in ascending order....................................15
21) Retrieve all customers ordered by their customer names in ascending order.................15
22) Retrieve all customers ordered by their customer names in descending order...............16
23) Group orders by customer_id and calculate the total amount for each customer...........17
24) Retrieve customer IDs and total order amounts for those customers who have placed
orders totaling more than 300....................................................................................................17
25) Retrieve all orders with their respective customer names...............................................18
26) Retrieve all customers with their respective orders. Include customers who do not have
any orders.20
27) Retrieve all orders with their respective customers. Include orders that do not have a
corresponding customer.............................................................................................................20
28) Retrieve all customers and their orders, including customers without orders and orders
without customers......................................................................................................................21
CONCLUSION..............................................................................................................................22

iii
PART I: Employees and their departments

1) Create a table named Employee with necessary constraints for each attribute.
(With columns: emp_id, name, department, salary , contact-no, address.)
CREATE TABLE Employee (
emp_id int AUTO_INCREMENT PRIMARY KEY,
name char(50) NOT NULL,
department varchar(100) NOT NULL,
salary decimal(10,2),
contact_no varchar(10),
address varchar(100));

2) Add a new column email to the Employee table.


ALTER TABLE employee ADD email varchar(30);

1
3) Modify the salary column to allow up to 12 digits.
**Following was done after (5) so name of table has changed**
ALTER TABLE staff_table
MODIFY COLUMN salary DECIMAL(14,2);

4) Rename the department column to dept_name.


ALTER TABLE employee CHANGE department dept_name varchar(100);

5) Rename the table Employee to staff table.


ALTER TABLE employee RENAME TO Staff_Table;

6) Delete the email column from the Employee table.


ALTER TABLE staff_table
DROP COLUMN email;

2
7) Delete all rows from the staff table
Adding rows/records in the table:
INSERT INTO staff_table(`name`,`dept_name`,`salary`,`contact_no`,`address`)
VALUES ('Pratik Raj Bista','Mathematics','25000','9812345678', 'ABCD Street Ktm' ),
('Prince Kumar Mandal','Computer','999999999999.99' , '9812344231' , 'FGHI
Tole Siraha' ),
('Piyush Poudel','Nepali', '100000' , '985432121', 'KLMN Village Lalitpur'),
('Aditya Lamichanne','Physcology', '200000', '9815432144', 'PQRS City Bhaktapur');

Deleting all rows from the table:


DELETE FROM staff_table;

8) Drop table staff.


DROP TABLE staff_table;

3
PART II: Customers and their orders

A. Create a table "customers" with fields (customer_id, customer_name) and fill three
data in the table.
Creating Table:
CREATE TABLE Customers (custormer_id int AUTO_INCREMENT PRIMARY KEY,
customer_name varchar(50) NOT NULL);

Inserting Data:
INSERT INTO CUSTOMERS (customer_name)
VALUES (‘Aalo Pandey’), (‘Giraula Bhindi’), (‘Cauli Bodhi’);

Note: 4th Customer is also needed to add the four values of order table, hence
INSERT INTO customers(`customer_name`) VALUES ('Potato Tomato');

4
B. Create another table "orders" with fields (order_id, order_date, customer_id, amount)
and fill following four data:

(101, 2023-01-10, 1, 100)(102, 2023-01-15, 2, 200)(103, 2023-01-20, 1, 150)

(104, 2023-01-25, 4, 300)

CREATE TABLE orders(


order_id int PRIMARY KEY,
order_date date,
customer_id int,
amount varchar(15),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);

INSERT INTO orders(`order_id`,`order_date`,`customer_id`,`amount`)


VALUES (101, '2023-01-10', 1, 100),
(102, '2023-01-15', 2, 200),
(103, '2023-01-20', 1, 150),
(104, '2023-01-25', 4, 300);

5
1) Retrieve all customers who have placed an order amounting to more than 150 and
the order date is in the year 2023.
SELECT customers.customer_name, orders.order_date, orders.amount
FROM customers
INNER JOIN orders
ON customers.customer_id= orders.customer_id
AND amount>150 AND order_date LIKE '2023-%';

2) Retrieve all customers who have placed an order amounting to more than 150 or the
order date is in the year 2023.
SELECT DISTINCT customers.customer_name, orders.order_date, orders.amount
FROM customers INNER JOIN orders
ON customers.customer_id= orders.customer_id
WHERE amount>150 OR order_date LIKE '2023-%';

6
3) Retrieve all orders where the order amount is not equal to 150.
SELECT c.customer_name, o.order_date, o.amount
FROM CUSTOMERS c
INNER JOIN orders o ON c.customer_id = o.customer_id
WHERE NOT amount= 150;

4) Calculate the average order amount for all orders placed in the year 2023.
SELECT AVG(`amount`) AS Average_Pricing
FROM orders WHERE `order_date` LIKE '2023-%';

7
5) Find the minimum order amount from the orders table.
SELECT Min(Order) As Min_Pricing FROM Orders;

6) Find the maximum order amount from the orders table.

SELECT MAX(Amount) AS Max_Pricing


FROM Orders;

8
7) Round the average order amount to 2 decimal places.
SELECT ROUND( AVG(amount),2) AS Avg_Fixed FROM ORDERS;

8) Retrieve all distinct order dates from the orders table.


SELECT DISTINCT order_date FROM ORDERS;

9
9) Calculate the total order amount for all orders placed by customer with ID 1.
SELECT SUM(`amount`) FROM orders
WHERE `customer_id`= 1;

10) Retrieve the length of the customer names from the customers table.
SELECT DISTINCT customer_name, CHAR_LENGTH(`customer_name`) AS string_length
FROM `customers`;

10
11) Convert all customer names to uppercase.
SELECT customer_name, UPPER (customer_name) FROM customers;

12) Convert all customer names to lowercase.


SELECT customer_name, LOWER(customer_name) FROM customers;

13) Concatenate the customer name and their customer ID.


SELECT CONCAT(customer_name, '_C', customer_id) FROM customers;

11
14) Reverse the customer names.
SELECT `customer_name`, Reverse(`customer_name`) AS Reversed_Name
FROM `customers`;

12
15) Repeat the customer names three times.
SELECT REPEAT(`customer_name`,3) FROM customers;

16) Retrieve the first three characters of each customer name.


SELECT `customer_name`, LEFT(`customer_name`,3) AS first_letters
FROM `customers`;

13
17) Retrieve the last three characters of each customer name.
SELECT `customer_name`, RIGHT(`customer_name`,3) AS first_letters
FROM `customers`;

18) Retrieve all customers whose names start with 'J'.


SELECT `customer_name` FROM customers
WHERE `customer_name` LIKE 'J%' ;

19) Retrieve all customers whose names have 'o' as the second character.
SELECT customer_name FROM customers
WHERE customer_name LIKE ‘ _o%’ ;

14
20) Retrieve all orders ordered by order amount in ascending order
SELECT * FROM `orders`
ORDER BY `amount` ASC ;

21) Retrieve all customers ordered by their customer names in ascending order.
SELECT * FROM customers
ORDER BY customer_name ASC;

15
22) Retrieve all customers ordered by their customer names in descending order.
SELECT * FROM `customers`
ORDER BY `customer_name` DESC;

16
23) Group orders by customer_id and calculate the total amount for each customer.
SELECT c.customer_name, SUM(o.amount) AS total_amount
FROM orders o
INNER JOIN customers c ON c.customer_id = o.customer_id
GROUP BY o.customer_id, c.customer_name
Note: Groups all orders by each unique customer(o.customerid), and for each group, show the
name c.name, and total amount. Only o.customer id didn’t work.

24) Retrieve customer IDs and total order


amounts for those customers who have placed orders totaling more than 300.
SELECT c.customer_id, c.customer_name, sum(o.amount) AS total_over300
FROM customers c JOIN orders o ON c.customer_id = o.customer_id
GROUP BY o.customer_id, c.customer_name
HAVING sum(o.amount)>=300; <<Having gives condition to GROUP BY and also because
WHERE cannot be used in an aggregate function weirdly >>

17
25) Retrieve all orders with their respective customer names.
SELECT c.customer_name, o.order_id, o.amount
FROM customers c
INNER JOIN orders o ON c.customer_id=o.customer_id;

18
**Inserting Extra Data for Questions 26) to 28)**
Customers who have no orders:

Orders with no customer id:

19
26) Retrieve all customers with their respective orders. Include customers who do not
have any orders.
SELECT c.customer_name, o.order_id, o.amount
FROM customers c
LEFT JOIN orders o
ON c.customer_id=o.customer_id;

27) Retrieve all orders with their respective customers. Include orders that do not have
a corresponding customer.
SELECT c.customer_name, o.order_id, o.amount FROM customers c RIGHT JOIN orders o
ON c.customer_id=o.customer_id;

20
28) Retrieve all customers and their orders, including customers without orders and
orders without customers.
SELECT c.customer_name, o.order_id, o.amount
FROM customers c LEFT JOIN orders o ON c.customer_id=o.customer_id
UNION
SELECT c.customer_name, o.order_id, o.amount
FROM customers c RIGHT JOIN orders o ON c.customer_id=o.customer_id;

21
CONCLUSION
From the above lab assignment, I learned the basic of SQL joining with its various types, along
with the use of varied Mathematical and String Functions, many of which were aggregates.
I learned to combine two or more tables relationally and use this to perform binary, or other
simple methods of data manipulation and data definition. This assignment taught me the value of
creative problem solving through use of simple tools given to a user.

22

You might also like