Database Lab 2
Database Lab 2
Theory: A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
LEFT (OUTER) JOIN: Returns all records from the left table, and the matched
records from the right table.
RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched
records from the left table.
PROCEDURE:
4. Now, we create the tables and add the value into the table to fulfill our objective of implementing
the “JOIN” clause in the database.
USE student;
SELECT s.std_id, s.std_name, c.course_id, c.course_name
FROM student s
JOIN course c ON s.course_id = c.course_id;
As we can see we have performed the “LEFT JOIN” which provides us with the
values of the student table dominant and course table values on the right side which
have been discarded.
USE student;
SELECT s.std_id, s.std_name, c.course_id, c.course_name
FROM student s
RIGHT JOIN course c ON s.course_id = c.course_id;
As we can see after the “RIGHT JOIN” the course table values are dominant than
the student table values.
CONCLUSION:
Hence, we have successfully implemented the
“JOIN” clause in our SQL database.
Lab no: 4 Date: 2024/ 08/ 08
Theory: A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
LEFT (OUTER) JOIN: Returns all records from the left table, and the matched
records from the right table.
RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched
records from the left table.
OBJECTIVE: Create a database with a Course Table, Students Table and Marks Table
and assign each students all the subjects.
KEY OBJECTIVE: Assign all subjects to all the Students in the Table.
PROCEDURE:
1. Firstly, we are using MySQL database for our practical.
2. Now, we use MySQL to create a database named “mbmc” and use the database to meet our
objective.
3. Firstly, creating the database and using it to create tables.
4. Now, we create the tables and add the value into the table to fulfill our objective of assigning
the subjects to the students.
5. Firstly, we create COURSE Table, STUDENTS Table and MARKS Table
respectively.
6. Now, inserting the values to work with and to meet our objectives.
CONCLUSION:
Hence, we met our objective and assigned all the course to all the students.
Lab no: 5 Date: 2024/ 08/ 09
Objective: Construct a database with Tables Customer (Cno, Cname, Caddress, Ccontact),
Purchase (Cno, Pid) & Product (Pid, Pname, price, quantity).
a. Find the names of all products having price 1000.
b. Find the name of those customers who purchased Dell Laptop
c. Find the total number of products purchased by customer ‘Ram’
d. Increase price of all products by 5%
e. Find total price of Apple Mobiles.
PROCEDURE:
5. Now, after the tables have been formed, we enter the value respectively in the tables.
TABLE CUSTOMER:
TABLE PRODUCT:
TABLE PURCHASE:
6. Now for the first objective “a” we solve it by running the following query:
USE RECORD;
SELECT COUNT(PH.PID) AS TOTALPRODUCTSPURCHASED
FROM PURCHASE PH
JOIN PRODUCT P ON PH.PID = P.PID
JOIN CUSTOMER C ON C.CNO = PH.CNO
WHERE C.CNAME = 'RAM';
9. Now for the objective no “d” we run the following query:
USE RECORD;
UPDATE PRODUCT
SET PRICE = PRICE * 1.05
WHERE PID IN (2001,2002,2003)
10. Now for the objective no “e” we run the following query:
USE RECORD;
SELECT SUM (PRICE * QUANTITY) AS TOTALPRICE
FROM PRODUCT
WHERE PNAME = 'APPLE MOBILE';
CONCLUSION:
Hence, we met our objective & displayed all the values and tasks asked by the
question.
Lab no: 6 Date: 2024/ 08/ 09
Objective: Retrieve Tname, Sname, Sphone for "ABC" school using SQL.
PROCEDURE:
TABLE SCHOOL:
TABLE TEACHER:
TABLE SCHOOL_TEACHER:
USE RECORD;
SELECT * FROM RECORD.SCHOOL_TEACHER;
INSERT INTO SCHOOL_TEACHER (NO_OF_PERIOD, SID, TID)
VALUES (5, 1001, 2001);
6. Now after inserting the respective values into the Tables, we join the Tables SCHOOL &
TEACHER with SCHOOL_TEACHER to fulfill our objective.
SELECT
T.TNAME,
S.SNAME,
S.SPHONE
FROM
SCHOOL_TEACHER ST
JOIN SCHOOL S ON S.SID=ST.SID
JOIN TEACHER T ON T.TID=ST.TID
WHERE
S.SNAME = 'ABC'
CONCLUSION:
Hence, we met our objective & retrieved Tname, Sname, Sphone for "ABC" school
using SQL.
Lab no: 7 Date: 2024/ 08/ 09
Objective: Consider a banking database with three labels and primary key underlined as given
below:
a) To display name of all customers who live in “Lalitpur” in ascending order of name.
b) To count total number of customers having loan at the bank.
c) To find name of those customers who have loan amount greater than or equal to 500000.
d) To find average loan amount of each accoun’t type.
PROCEDURE:
4. After creating the database, we are creating tables necessary for meeting our
objectives.
5. After creating the tables, we are going to insert the values into the table necessary for
meeting our objectives.
TABLE CUSTOMER:
TABLE LOAN:
TABLE BORROW:
USE BANKING;
SELECT * FROM BANKING.BORROW;
INSERT INTO BORROW (CID, LOAN_NUMBER) VALUES
(1, 1000), (2,1001), (3,1002), (4,1003);
6. Now, after the values have been inserted, we are going to insert the queries required to meet
our objectives.
a) For meeting the objective no ‘a’ we are going to enter the following query:
SELECT COUNT(C.CID)
FROM BORROW B
JOIN CUSTOMER C ON B.CID=C.CID
JOIN LOAN L ON B.LOAN_NUMBER=L.LOAN_NUMBER
c) For meeting the objective no ‘c’ we are going to enter the following query:
SELECT C.CNAME
FROM BORROW B
JOIN CUSTOMER C ON C.CID=B.CID
JOIN LOAN L ON L.LOAN_NUMBER=B.LOAN_NUMBER
WHERE L.LAMOUNT>=500000
d) For meeting the objective no ‘d’ we are going to enter the following query:
CONCLUSION:
Hence, all the objectives that was provided by the question have been
met.