0% found this document useful (0 votes)
24 views

DBMsnew

Uploaded by

amitybhavya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

DBMsnew

Uploaded by

amitybhavya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

EXPERIMENT -7

AIM- Consider the following relational database.Give expressions in QBE for each of the
following SQL
queries.Employee(personname,street,city)Works(personname,companyname,salary)Company(
companyname,city)Manages(personname,managername)Modify the database so that Jones
now lives in Newtown.Give all employees of First Bank Corporation a 10 % raise.Give all
mangers in the database a 10% raise.Give all mangers in the database a 10 % raise unless the
salary would be greater than Rs.100,000. In such a case give only a 3% raise.
SOFTWARE USED- MySQL
//CODE
CREATE DATABASE assignmnet1;
use assignmnet1;
CREATE TABLE Employee_12 (
personname VARCHAR(50),
street VARCHAR(50),
city VARCHAR(50),
PRIMARY KEY (personname));
-- Create the Works table
CREATE TABLE Works1 (
personname VARCHAR(50),
companyname VARCHAR(50),
salary DECIMAL(10, 2),
FOREIGN KEY (personname) REFERENCES Employee_12(personname));
-- Create the Company table
CREATE TABLE Company1(
companyname VARCHAR(50),
city VARCHAR(50),
PRIMARY KEY (companyname));
-- Create the Manages table
CREATE TABLE Manages1(
personname VARCHAR(50),
managername VARCHAR(50),
FOREIGN KEY (personname) REFERENCES Employee_12(personname),
FOREIGN KEY (managername) REFERENCES Employee_12(personname));
-- Insert data into the Employee table
INSERT INTO Employee_12 (personname, street, city) VALUES
('John', '123 Elm St', 'Chicago'),
('Alice', '456 Oak St', 'Boston'),
('Jones', '789 Maple St', 'New York'),
('Robert', '111 Pine St', 'San Francisco');
--Insert data into the Works table
INSERT INTO Works1 (personname, companyname, salary) VALUES
('John', 'First Bank Corporation', 50000),
('Alice', 'Tech Solutions', 80000),
('Jones', 'First Bank Corporation', 60000),
('Robert', 'Retail Corp', 90000);
-- Insert data into the Company table
INSERT INTO Company1 (companyname, city) VALUES
('First Bank Corporation', 'New York'),
('Tech Solutions', 'Boston'),
('Retail Corp', 'San Francisco');
-- Insert data into the Manages table
INSERT INTO Manages1 (personname, managername) VALUES
('John', 'Jones'),
('Alice', 'Robert');
-- Update Jones' city to Newtown
UPDATE Employee_12
SET city = 'Newtown'
WHERE personname = 'Jones';
SELECT * FROM Employee_12;

-- Give all employees of First Bank Corporation a 10% raise


SET SQL_SAFE_UPDATES = 0;
UPDATE Works1
SET salary = salary * 1.10
WHERE companyname = 'First Bank Corporation';
SELECT * FROM Works1;

-- Give all managers a 10% raise


UPDATE Works1
SET salary = salary * 1.10
WHERE personname IN (SELECT managername FROM Manages1);
SELECT * FROM Works1;
-- Give all managers a 10% raise unless the salary would be greater than Rs.100,000. In that
case, give only a 3% raise
UPDATE Works1
SET salary = CASE
WHEN salary * 1.10 <= 100000 THEN salary * 1.10
ELSE salary * 1.03
END
WHERE personname IN (SELECT managername FROM Manages1);
SELECT * FROM Works1;
OUTPUT-

RESULT- The query has been implemented successfully.


EXPERIMENT -8
AIM-Consider the following relation patients (pnum, pname, age) doctors (dnum, dname, rank)
visits (pnum, dnum, date, diagnosis) Construct the SQL queries for the following i. Who are the
patients 10 years old or younger? ii. Who are the surgeons iii. What are the phone numbers of
doctors iv. What are the phone numbers of surgeons?Create a SQL query.
SOFTWARE USED- MySQL
//CODE
create database doctordetails;
use doctordetails;
CREATE TABLE patients (
pnum INT PRIMARY KEY,
pname VARCHAR(50),
age INT);
-- Create the doctors table with a phone number column
CREATE TABLE doctors (
dnum INT PRIMARY KEY,
dname VARCHAR(50),
ranks varchar(50),
phone VARCHAR(15) -- Adding a column for phone numbers);
-- Create the visits table
CREATE TABLE visits (
pnum INT,
dnum INT,
visit_date DATE,
diagnosis VARCHAR(255),
FOREIGN KEY (pnum) REFERENCES patients(pnum),
FOREIGN KEY (dnum) REFERENCES doctors(dnum));
-- Insert sample data into the patients table
INSERT INTO patients (pnum, pname, age) VALUES
(1, 'Alice', 5),
(2, 'Bob', 12),
(3, 'Charlie', 9),
(4, 'David', 15),
(5, 'Eve', 10);
-- insert sample data into the doctors table
INSERT INTO doctors (dnum, dname, ranks, phone) VALUES
(1, 'Dr. Smith', 'Surgeon', '123-456-7890'),
(2, 'Dr. Adams', 'General Practitioner', '987-654-3210'),
(3, 'Dr. Johnson', 'Surgeon', '555-555-5555'),
(4, 'Dr. White', 'Pediatrician', '111-222-3333');
-- Insert sample data into the visits table
INSERT INTO visits (pnum, dnum, visit_date, diagnosis) VALUES
(1, 1, '2024-01-01', 'Flu'),
(2, 2, '2024-02-15', 'Cold'),
(3, 3, '2024-03-10', 'Fracture'),
(4, 4, '2024-04-20', 'Check-up'),
(5, 1, '2024-05-30', 'Allergy');
DESCRIBE patients;
SELECT pname FROM patients WHERE age <= 10;

SELECT dname FROM doctors WHERE ranks = 'Surgeon';


SELECT dname, phone FROM doctors;

SELECT dname, phone FROM doctors WHERE ranks = 'Surgeon';

RESULT- The query has been implemented successfully.


EXPERIMENT -1

AIM- Explore and explain various types of database languages such as DDL, DML, DCL & TCL.

SOFTWARE USED- MySQL

THEORY-

->DATA DEFINITION LANGUAGE (DDL)- This is used to create tables, indexes, schemas and
constraints. With the help of DDL we can define the dbms internal structure and pattern of the
database.

1.CREATE- used to create objects in the database.

2.ALTER-used to change or alter the structure of the database objects.

3.RENAME-used to rename the object in the database.


4.COMMENT-used to add comment to the data database.

//single line comment

//multi-line comment

5.DROP-used to delete table from the database.


6.TRUNCATE-used to remove all records from the database.

->DATA CONTROL LANGUAGE (DCL) -these commands are used to retrieve the saved data
from the database.

1.GRANT- it is used to give user access to the database.

2.REVOKE- it is used to take back the user access.


->DATA MANIPULATION LANGUAGE (DML)- used to Manipulate the data in the database by
using different commands.

1.SELECT-used to select data from the table.

2.INSERT- used for Inserting data into an existing table.

3.UPDATE-used to update data in the Table based on the requirement.


4.DELETE-used to delete data from the Table.

5.MERGE-used for upsert operations

MERGE INTO target_table AS target


USING source_table AS source
ON (target.id = source.id)
WHEN MATCHED THEN
UPDATE SET target.name = source.name
WHEN NOT MATCHED THEN
INSERT (id, name) VALUES (source.id, source.name);

6.CALL-used to call a structured query language or Java sub-progra

CALL user_defined_function(parameter 1, parameter 2);

7.LOCK TABLE-it can control the concurrency

LOCK TABLE your_table IN EXCLUSIVE MODE;

TRANSACTIONAL CONTROL LANGUAGE(TCL)- commands are used to run the changes made
by the DML commands one more thing is TCL can be grouped into a logical transaction.
1.COMMIT: It is used to save the transaction on the Database. These are very useful in the
banking sector.

2.ROLLBACK: It is used to restore the database to its original state from the last commit. This
command also plays an important role in Banking Sectors.

RESULT- The query has been successfully implemented.


EXPERIMENT 3

AIM- Explain foreign key and primary key in SQL

SOFTWARE USED- MySQL

THEORY-

PRIMARY KEY- A primary key in a table that uniquely identifies each row and column or set of
columns in the table. The primary key is an attribute or a set of attributes that help to
uniquely identify the tuples(records) in the relational table. The primary key which refers to a
nonnull value that is required for the identification of the tuple (record) means that any
attribute can not contain the non-null value in DBMS. The primary key in which value can not
be duplicated in any of the rows or tuples of the relation.

FOREIGN KEY- A Foreign Key is a column in one table that matches a Primary Key in another
table, allowing the two tables to be connected together . A foreign key also maintains
referential integrity between two tables, making it impossible to drop the table containing the
primary key. In a foreign key reference, a link is created between two tables when the column
or columns that hold the primary key value for one table are referenced by the column or
columns in another table. This column becomes a foreign key in the second table.
RESULT- The query has been successfully implemented.
EXPERIMENT 9

AIM- Create a database called COMPANY consisting of two tables - EMP & DEPT EMP. Perform
the given queries on the tables just created.

SOFTWARE USED- MySQL

//CODE

CREATE DATABASE DBMS1;

USE DBMS1;

-- Step 2: Create the EMP table

CREATE TABLE EMP (

EMPNO CHAR(50) PRIMARY KEY,

ENAME VARCHAR(50),

JOB CHAR(10),

MGR CHAR(50),

HIREDATE DATE,

SAL CHAR(50),

COMM CHAR(59),

DEPTNO CHAR(40));

-- Step 3: Create the DEPT table

CREATE TABLE DEPT (

DEPTNO CHAR(40) PRIMARY KEY,

DNAME VARCHAR(50),

LOC VARCHAR(50));

-- Step 4: Insert data into the EMP table


INSERT INTO EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES

(7369, 'Smith', 'Clerk', 7902, '1980-12-17', 800, NULL, 20),

(7499, 'Allen', 'Salesman', 7698, '1981-02-20', 1600, 300, 30),

(7521, 'Ward', 'Salesman', 7698, '1981-02-22', 1250, 500, 30),

(7566, 'Jones', 'Manager', 7839, '1981-04-02', 2975, NULL, 20),

(7654, 'Martin', 'Salesman', 7698, '1981-09-28', 1250, 1400, 30),

(7698, 'Blake', 'Manager', 7839, '1981-05-01', 2850, NULL, 30),

(7782, 'Clark', 'Manager', 7839, '1981-06-09', 2450, NULL, 10),

(7788, 'Scott', 'Analyst', 7566, '1982-12-09', 3000, NULL, 20),

(7839, 'King', 'President', NULL, '1981-11-17', 5000, NULL, 10),

(7844, 'Turner', 'Salesman', 7698, '1981-09-08', 1500, 0, 30),

(7876, 'Adams', 'Clerk', 7788, '1983-01-12', 1100, NULL, 20),

(7900, 'James', 'Clerk', 7698, '1981-12-03', 950, NULL, 30),

(7902, 'Ford', 'Analyst', 7566, '1981-12-04', 3000, NULL, 20),

(7934, 'Miller', 'Clerk', 7782, '1982-01-23', 1300, NULL, 10);

-- Step 5: Insert data into the DEPT table

INSERT INTO DEPT (DEPTNO, DNAME, LOC) VALUES

(10, 'Accounting', 'New York'),

(20, 'Research', 'Dallas'),

(30, 'Sales', 'Chicago'),

(40, 'Operations', NULL);

-- Step 6: List the names of analysts and salesmen

SELECT ENAME FROM EMP


WHERE JOB IN ('Analyst', 'Salesman');

-- Step 7: List details of employees who have joined before 30 Sep 81

SELECT * FROM EMP

WHERE HIREDATE < '1981-09-30';

-- Step 8: List names of employees who are not managers

SELECT ENAME FROM EMP

WHERE JOB <> 'Manager';


-- Step 9: List the names of employees whose employee numbers are 7369, 7521, 7839,
7934, 7788

SELECT ENAME FROM EMP

WHERE EMPNO IN (7369, 7521, 7839, 7934, 7788);

-- Step 10: List employees not belonging to department 30, 40, or 10

SELECT ENAME FROM EMP

WHERE DEPTNO NOT IN (30, 40, 10);

-- Step 11: List employee names for those who have joined between 30 June and 31 Dec. 1981

SELECT ENAME FROM EMP

WHERE HIREDATE BETWEEN '1981-06-30' AND '1981-12-31';


-- Step 12: List the different designations in the company

SELECT DISTINCT JOB

FROM EMP;

-- Step 13: List the names of employees who are not eligible for commission

SELECT ENAME

FROM EMP

WHERE COMM IS NULL;

-- Step 14: List the name and designation of the employee who does not report to anybody

SELECT ENAME, JOB

FROM EMP

WHERE MGR IS NULL;


-- Step 15: List the employees not assigned to any department

SELECT ENAME FROM EMP

WHERE DEPTNO IS NULL;

-- Step 16: List the employees who are eligible for commission

SELECT ENAME FROM EMP

WHERE COMM IS NOT NULL;

-- Step 17: List employees whose names either start or end with “S”

SELECT ENAME
FROM EMP

WHERE ENAME LIKE 'S%' OR ENAME LIKE '%S';

-- Step 18: List names of employees whose names have “i” as the second character

SELECT ENAME

FROM EMP

WHERE ENAME LIKE '_i%';

-- Step 19: List the number of employees working with the company

SELECT COUNT(*)

FROM EMP;

-- Step 20: List the number of designations available in the EMP table

SELECT COUNT(DISTINCT JOB)

FROM EMP;
-- Step 21: List the total salaries paid to the employees

SELECT SUM(SAL)

FROM EMP;

-- Step 22: List the maximum, minimum, and average salary in the company

SELECT MAX(SAL) AS Max_Salary, MIN(SAL) AS Min_Salary, AVG(SAL) AS Avg_Salary

FROM EMP;

-- Step 23: List the maximum salary paid to a salesman

SELECT MAX(SAL)

FROM EMP WHERE JOB = 'Salesman’;

RESULT- The query has been implemented successfully.


EXPERIMENT 2

AIM- To explore and implement SQL functions and understand their role in data manipulation.

SOFTWARE USED- MySQL

THEORY-

SQL Functions are built-in programs that are used to perform different operations on the
database.
There are two types of functions in SQL:
1.Aggregate Functions
2.Scalar Functions.

1. AGGREGATE FUNCTIONS- SQL functions can operate on a data group and return a singular output.
2. SQL Scalar Functions are built-in functions that operate on a single value and return a single
value.
OUTPUT-
RESULT- The query has been implemented successfully.
EXPERIMENT- 4

AIM- Write the subqueries of client and product tables.

SOFTWARE USED- MySQL

//CODE

/*client master table*/

CREATE DATABASE MEMORY;

USE MEMORY;

CREATE TABLE CLIENT_MASTER_11(

CLIENTNO varchar(6) primary key,

NAME varchar(20) not null,

ADDRESS1 varchar(30),

ADDRESS2 varchar(30),

CITY varchar(15),

PINCODE int,

STATE varchar(15),

BALDUE decimal(10,2));

ALTER TABLE Client_Master_11

ADD CONSTRAINT ck_client CHECK (ClientNo LIKE 'C%');

INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue) VALUES
('C00001', 'Ivan Bayross', 'Mumbai', 400054, 'Maharashtra', 15000);

INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue) VALUES
('C00002', 'Mamta Muzumdar', 'Madras', 780001, 'Tamil Nadu', 0);

INSERT INTO Client_Master_11 (ClientNo, Name, City, Pincode, State, BalDue) VALUES
('C00003', 'Chhaya Bankar', 'Mumbai', 400057, 'Maharashtra', 5000);
INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue)VALUES
('C00004', 'Ashwini Joshi', 'Bangalore', 560001, 'Karnataka', 0);

INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue)VALUES


('C00005', 'Hansel Colaco', 'Mumbai', 400060, 'Maharashtra', 2000);

INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue) VALUES
('C00006', 'Deepak Sharma', 'Mangalore', 560050, 'Karnataka', 0);

SELECT * FROM CLIENT_MASTER_11;

SELECT Name ,Address1, Address2, City, State, PinCode FROM Client_Master_11 WHERE
ClientNo

IN(SELECT ClientNo FROM Sales_Order WHERE OrderNo = 'O19001');

SELECT ClientNo, Name

FROM Client_Master_11

WHERE ClientNo IN (

SELECT ClientNo

FROM Sales_Order

WHERE DATE_FORMAT(OrderDate, '%b,%y') < 'May,02');

SELECT Name FROM Client_Master_11

WHERE ClientNo IN(SELECT ClientNo FROM Sales_Order

WHERE OrderNo IN(SELECT OrderNo FROM Sales_Order_Details

WHERE (QtyOrdered * ProductRate) >= 10000));

SELECT ClientNo, Name FROM Client_Master_11

WHERE ClientNo IN(SELECT ClientNo FROM Sales_Order


WHERE OrderNo IN(SELECT OrderNo FROM Sales_Order_Details

WHERE ProductNo IN(SELECT ProductNo FROM Product_Master_121

WHERE Description = 'Lycra Tops')));

/* product master table*/

CREATE TABLE PRODUCT_MASTER_121(

PRODUCTNO varchar(6),

DESCRIPTION varchar(15),

PROFITPERCENT decimal(4,2),

UNITMEASURE varchar(10),

QTYONHAND int, REORDERLVL int,

SELLPRICE decimal(8,2), COSTPRICE decimal(8,2));

ALTER TABLE PRODUCT_MASTER_11

ADD CONSTRAINT ck_product CHECK (PRODUCTNO like 'P%'),

ADD CONSTRAINT ck_sell CHECK (SELLPRICE <> 0),

ADD CONSTRAINT ck_cost CHECK (COSTPRICE <> 0);

INSERT INTO Product_Master_121 VALUES ('P00001', 'T-Shirts', 5, 'Piece', 200, 50, 350, 250);

INSERT INTO Product_Master_121 VALUES ('P03453', 'Shirts', 6, 'Piece', 150, 50, 500, 350);

INSERT INTO Product_Master_121 VALUES ('P06734', 'Cotton Jeans', 5, 'Piece', 100, 20, 600,
450);

INSERT INTO Product_Master_121 VALUES ('P07865', 'Jeans', 5, 'Piece', 100, 20, 750, 500);

INSERT INTO Product_Master_121 VALUES ('P07868', 'Trousers', 2, 'Piece', 150, 50, 850, 550);

INSERT INTO Product_Master_121 VALUES ('P07885', 'Pull Overs', 2.5, 'Piece', 80, 30, 700, 450);

INSERT INTO Product_Master_121 VALUES ('P07965', 'Denim Shirts', 4, 'Piece', 100, 40, 350,
250);
INSERT INTO Product_Master_121 VALUES ('P07975', 'Lycra Tops', 5, 'Piece', 70, 30, 300, 175);

INSERT INTO Product_Master_121 VALUES ('P08865', 'Skirts', 5, 'Piece', 75, 30, 450, 300);

SELECT * FROM PRODUCT_MASTER_121;

SELECT ProductNo, Description FROM Product_Master_11 WHERE ProductNo

NOT IN(SELECT ProductNo FROM Sales_Order_Details);

/*salesman table*/

CREATE TABLE SALESMAN_MASTER_11(

SALESMANNO varchar(6), SALESMANNAME varchar(20),

ADDRESS1 varchar(30), ADDRESS2 varchar(30),

CITY varchar(20), PINCODE int,

STATE varchar(20), SALAMT decimal(8,2),

TGTTOGET decimal(6,2), YTDSALES decimal(6,2),

REMARKS varchar(60));

ALTER TABLE SALESMAN_MASTER_11

ADD CONSTRAINT ck_salesman CHECK (SALESMANNO like 'S%'),

ADD CONSTRAINT ck_sal CHECK (SALAMT <> 0),

ADD CONSTRAINT ck_target CHECK (TGTTOGET <> 0);

INSERT INTO Salesman_Master_11 VALUES ('S00001', 'Aman', 'A/14', 'Worli', 'Mumbai',


400002, 'Maharashtra', 3000, 100, 50, 'Good');

INSERT INTO Salesman_Master_11 VALUES ('S00002', 'Omkar', '65', 'Nariman', 'Mumbai',


400001, 'Maharashtra', 3000, 200, 100, 'Good');

INSERT INTO Salesman_Master_11 VALUES ('S00003', 'Raj', 'P-7', 'Bandra', 'Mumbai', 400032,
'Maharashtra', 3000, 200, 100, 'Good');

INSERT INTO Salesman_Master_11 VALUES ('S00004', 'Ashish', 'A/5', 'Juhu', 'Bombay', 400044,
'Maharashtra', 3500, 200, 150, 'Good');
SELECT * FROM SALESMAN_MASTER_11;

/*salesorder table*/

CREATE TABLE SALES_ORDER(

ORDERNO varchar(6) PRIMARY KEY,

CLIENTNO varchar(6) REFERENCES CLIENT_MASTER,

ORDERDATE date, DELYADDR varchar(25),

SALESMANNO varchar(6)REFERENCES SALESMAN_MASTER,

DELYTYPE char(1) DEFAULT 'F', BILLEDYN char(1),

DELYDATE date, ORDERSTATUS varchar(10),

CONSTRAINT ck_order CHECK (ORDERNO like 'O%'),

CONSTRAINT ck_dely_type CHECK (DELYTYPE IN ('P', 'F')),

CONSTRAINT ck_ord_status

CHECK(ORDERSTATUS IN ('In Process', 'Fulfilled', 'Backorder', 'Cancelled')));

INSERT INTO Sales_Order (OrderNo, OrderDate, ClientNo, DelyType, BilledYn, SalesmanNo,


DelyDate, OrderStatus)

VALUES ('O19001', '2002-06-12', 'C00001', 'F', 'N', 'S00001', '2002-07-20', 'In Process');

INSERT INTO Sales_Order (OrderNo, OrderDate, ClientNo, DelyType, BilledYn, SalesmanNo,


DelyDate, OrderStatus)

VALUES ('O19002', '2002-06-25', 'C00002', 'P', 'N', 'S00002', '2002-07-27', 'Cancelled');

INSERT INTO Sales_Order (OrderNo, OrderDate, ClientNo, DelyType, BilledYn, SalesmanNo,


DelyDate, OrderStatus)

VALUES ('O19003', '2002-02-18', 'C00003', 'F', 'Y', 'S00003', '2002-02-20', 'Fulfilled');

INSERT INTO Sales_Order (OrderNo, OrderDate, ClientNo, DelyType, BilledYn, SalesmanNo,


DelyDate, OrderStatus)

VALUES ('O19004', '2002-04-03', 'C00001', 'F', 'Y', 'S00001', '2002-04-07', 'Fulfilled');


INSERT INTO Sales_Order (OrderNo, OrderDate, ClientNo, DelyType, BilledYn, SalesmanNo,
DelyDate, OrderStatus)

VALUES ('O46866', '2002-05-20', 'C00004', 'P', 'N', 'S00002', '2002-05-22', 'Cancelled');

INSERT INTO Sales_Order (OrderNo, OrderDate, ClientNo, DelyType, BilledYn, SalesmanNo,


DelyDate, OrderStatus)

VALUES ('O19008', '2002-05-24', 'C00005', 'F', 'N', 'S00004', '1996-07-26', 'In Process');

SELECT * FROM SALES_ORDER;

SELECT ClientNo, Name FROM Client_Master

WHERE ClientNo IN(SELECT ClientNo FROM Sales_Order

WHERE OrderNo IN(SELECT OrderNo FROM Sales_Order_Details

WHERE ProductNo IN(SELECT ProductNo FROM Product_Master

WHERE Description = 'Lycra Tops')));

/*salesorder details table*/

CREATE TABLE SALES_ORDER_DETAILS(

ORDERNO varchar(6) REFERENCES SALES_ORDER,

PRODUCTNO varchar(6) REFERENCES PRODUCT_MASTER,

QTYORDERED int, QTYDISP int, PRODUCTRATE decimal(10,2),

PRIMARY KEY (ORDERNO, PRODUCTNO));

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19001', 'P00001', 4, 4, 525);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19001', 'P07965', 2, 1, 8400);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19001', 'P07885', 2, 1, 5250);
INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)
VALUES('O19002', 'P00001', 10, 0, 525);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O46865', 'P07868', 3, 3, 3150);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O46865', 'P07885', 3, 1, 5250);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O46865', 'P00001', 10, 10, 525);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O46865', 'P03453', 4, 4, 1050);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19003', 'P03453', 2, 2, 1050);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19003', 'P06734', 1, 1, 12000);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O46866', 'P07965', 1, 0, 8400);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O46866', 'P07975', 1, 0, 1050);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19008', 'P00001', 10, 5, 525);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19008', 'P07975', 5, 3, 1050);

SELECT * FROM SALES_ORDER_DETAILS;

SELECT SOD.ProductNo, PM.Description

FROM Sales_Order_Details SOD, Sales_Order SO, Product_Master PM,

Client_Master CM

WHERE PM.ProductNo = SOD.ProductNo AND SO.OrderNo = SOD.OrderNo

AND CM.ClientNo = SO.ClientNo AND CM.Name = 'Ivan Bayross';


SELECT SOD.ProductNo, PM.Description, SUM(SOD.QtyOrdered) AS TotalQty

FROM Sales_Order_Details SOD

JOIN Sales_Order SO ON SO.OrderNo = SOD.OrderNo

JOIN Product_Master_121 PM ON PM.ProductNo = SOD.ProductNo

WHERE DATE_FORMAT(SO.DelyDate, '%b-%y') = DATE_FORMAT(NOW(), '%b-%y')

GROUP BY SOD.ProductNo, PM.Description;

SELECT DISTINCT Product_Master_121.ProductNo, Description

FROM Sales_Order_Details, Product_Master_121

WHERE Product_Master_121.ProductNo =Sales_Order_Details.ProductNo;

SELECT DISTINCT Sales_Order.ClientNo, Client_Master.Name

FROM Sales_Order_Details, Sales_Order, Product_Master, Client_Master

WHERE Product_Master.ProductNo = Sales_Order_Details.ProductNo

AND Sales_Order.OrderNo = Sales_Order_Details.OrderNo

AND Client_Master.ClientNo = Sales_Order.ClientNo

AND Description = 'Trousers';

SELECT Sales_Order_Details.ProductNo, Sales_Order_Details.OrderNo

FROM Sales_Order_Details, Sales_Order, Product_Master_121

WHERE Sales_Order.OrderNo = Sales_Order_Details.OrderNo

AND Product_Master_121.ProductNo = Sales_Order_Details.ProductNo


AND Sales_Order_Details.QtyOrdered < 5

AND Product_Master_121.Description = 'Pull Overs';

SELECT SOD.ProductNo, PM.Description, SUM(QtyOrdered) 'Units Ordered'

FROM Sales_Order_Details SOD, Sales_Order SO, Product_Master_121 PM,

Client_Master_11 CM

WHERE SO.OrderNo = SOD.OrderNo AND PM.ProductNo = SOD.ProductNo

AND CM.ClientNo = SO.ClientNo

AND (CM.Name = 'Ivan Bayross' OR CM.Name = 'Mamta Muzumdar')

GROUP BY SOD.ProductNo, PM.Description;

SELECT SO.ClientNo, SOD.ProductNo, PM.Description, SUM(QtyOrdered) 'Units Ordered'

FROM Sales_Order SO, Sales_Order_Details SOD, Product_Master_121 PM,

Client_Master_11 CM

WHERE SO.OrderNo = SOD.OrderNo AND SOD.ProductNo = PM.ProductNo

AND SO.ClientNo = CM.ClientNo

GROUP BY SO.ClientNo, SOD.ProductNo, PM.Description

HAVING SO.ClientNo = 'C00001' OR SO.ClientNo='C00002';

SELECT description, SUM(QtyDisp) FROM Product_Master, Sales_Order_Details

WHERE Product_Master.ProductNo = Sales_Order_Details.ProductNo

GROUP BY Description;
SELECT Sales_Order_Details.ProductNo, Product_Master.Description,

SUM(Sales_Order_Details.QtyDisp * Sales_Order_Details.ProductRate) 'Sales Per

Product'

FROM Sales_Order_Details, Product_Master

WHERE Product_Master.ProductNo = Sales_Order_Details.ProductNo

GROUP BY Sales_Order_Details.ProductNo, Product_Master.Description;

SELECT CM.ClientNo, CM.Name, AVG(SOD.QtyDisp) 'Avg. Sales'

FROM Sales_Order_Details SOD, Sales_Order SO, Client_Master CM

WHERE CM.ClientNo = SO.ClientNo AND SO.OrderNo = SOD.OrderNo

GROUP BY CM.ClientNo, Name

HAVING MAX(SOD.QtyOrdered * SOD.ProductRate) > 15000;

SELECT SO.OrderNo, SO.OrderDate, SUM(SOD.QtyOrdered * SOD.ProductRate) 'Order Billed'

FROM Sales_Order SO, Sales_Order_Details SOD

WHERE SOD.OrderNo = SO.OrderNo AND SO.Billed = 'Y'

AND to_char(OrderDate, 'MON') = 'Jun'

GROUP BY SO.OrderNo;

OUTPUT-

1. Finding the non- moving products .i.e. products not being sold.
2. Finding the name and complete address for the customer who has placed order number
‘O19001’.

3. Finding the clients who have placed orders before the month of May'02.

4. Find out if the product 'Lycra Tops' has been ordered by any client and print the ClientNo,
Name to whom it was sold.
5. Find the names of clients who have placed orders worth Rs. 10000 or more.

RESULT- SQL Subqueries have been studied and implemented successfully.


EXPERIMENT -5

AIM- Write group by queries of client and product tables.

THEORY- The GROUP BY Statement in SQL is used to arrange identical data into groups with
the help of some functions. i.e. if a particular column has the same values in different rows
then it will arrange these rows in a group.

SOFTWARE USED- MySQL

//CODE

CREATE DATABASE MEMORY;

USE MEMORY;

CREATE TABLE CLIENT_MASTER_11(

CLIENTNO varchar(6) primary key,

NAME varchar(20) not null,

ADDRESS1 varchar(30),

ADDRESS2 varchar(30),

CITY varchar(15),

PINCODE int,

STATE varchar(15),

BALDUE decimal(10,2));

ALTER TABLE Client_Master_11

ADD CONSTRAINT ck_client CHECK (ClientNo LIKE 'C%');

INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue) VALUES
('C00001', 'Ivan Bayross', 'Mumbai', 400054, 'Maharashtra', 15000);

INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue) VALUES
('C00002', 'Mamta Muzumdar', 'Madras', 780001, 'Tamil Nadu', 0);
INSERT INTO Client_Master_11 (ClientNo, Name, City, Pincode, State, BalDue) VALUES
('C00003', 'Chhaya Bankar', 'Mumbai', 400057, 'Maharashtra', 5000);

INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue)VALUES


('C00004', 'Ashwini Joshi', 'Bangalore', 560001, 'Karnataka', 0);

INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue)VALUES


('C00005', 'Hansel Colaco', 'Mumbai', 400060, 'Maharashtra', 2000);

INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue) VALUES
('C00006', 'Deepak Sharma', 'Mangalore', 560050, 'Karnataka', 0);

SELECT * FROM CLIENT_MASTER_11;

SELECT Name ,Address1, Address2, City, State, PinCode FROM Client_Master_11 WHERE
ClientNo

IN(SELECT ClientNo FROM Sales_Order WHERE OrderNo = 'O19001');

SELECT ClientNo, Name

FROM Client_Master_11

WHERE ClientNo IN (

SELECT ClientNo

FROM Sales_Order

WHERE DATE_FORMAT(OrderDate, '%b,%y') < 'May,02');

SELECT Name FROM Client_Master_11

WHERE ClientNo IN(SELECT ClientNo FROM Sales_Order

WHERE OrderNo IN(SELECT OrderNo FROM Sales_Order_Details

WHERE (QtyOrdered * ProductRate) >= 10000));


SELECT ClientNo, Name FROM Client_Master_11

WHERE ClientNo IN(SELECT ClientNo FROM Sales_Order

WHERE OrderNo IN(SELECT OrderNo FROM Sales_Order_Details

WHERE ProductNo IN(SELECT ProductNo FROM Product_Master_121

WHERE Description = 'Lycra Tops')));

/* product master table*/

CREATE TABLE PRODUCT_MASTER_121(

PRODUCTNO varchar(6),

DESCRIPTION varchar(15),

PROFITPERCENT decimal(4,2),

UNITMEASURE varchar(10),

QTYONHAND int, REORDERLVL int,

SELLPRICE decimal(8,2), COSTPRICE decimal(8,2));

ALTER TABLE PRODUCT_MASTER_11

ADD CONSTRAINT ck_product CHECK (PRODUCTNO like 'P%'),

ADD CONSTRAINT ck_sell CHECK (SELLPRICE <> 0),

ADD CONSTRAINT ck_cost CHECK (COSTPRICE <> 0);

INSERT INTO Product_Master_121 VALUES ('P00001', 'T-Shirts', 5, 'Piece', 200, 50, 350, 250);

INSERT INTO Product_Master_121 VALUES ('P03453', 'Shirts', 6, 'Piece', 150, 50, 500, 350);

INSERT INTO Product_Master_121 VALUES ('P06734', 'Cotton Jeans', 5, 'Piece', 100, 20, 600,
450);

INSERT INTO Product_Master_121 VALUES ('P07865', 'Jeans', 5, 'Piece', 100, 20, 750, 500);

INSERT INTO Product_Master_121 VALUES ('P07868', 'Trousers', 2, 'Piece', 150, 50, 850, 550);
INSERT INTO Product_Master_121 VALUES ('P07885', 'Pull Overs', 2.5, 'Piece', 80, 30, 700, 450);

INSERT INTO Product_Master_121 VALUES ('P07965', 'Denim Shirts', 4, 'Piece', 100, 40, 350,
250);

INSERT INTO Product_Master_121 VALUES ('P07975', 'Lycra Tops', 5, 'Piece', 70, 30, 300, 175);

INSERT INTO Product_Master_121 VALUES ('P08865', 'Skirts', 5, 'Piece', 75, 30, 450, 300);

SELECT * FROM PRODUCT_MASTER_121;

SELECT ProductNo, Description FROM Product_Master_11 WHERE ProductNo

NOT IN(SELECT ProductNo FROM Sales_Order_Details);

/*salesman table*/

CREATE TABLE SALESMAN_MASTER_11(

SALESMANNO varchar(6), SALESMANNAME varchar(20),

ADDRESS1 varchar(30), ADDRESS2 varchar(30),

CITY varchar(20), PINCODE int,

STATE varchar(20), SALAMT decimal(8,2),

TGTTOGET decimal(6,2), YTDSALES decimal(6,2),

REMARKS varchar(60));

ALTER TABLE SALESMAN_MASTER_11

ADD CONSTRAINT ck_salesman CHECK (SALESMANNO like 'S%'),

ADD CONSTRAINT ck_sal CHECK (SALAMT <> 0),

ADD CONSTRAINT ck_target CHECK (TGTTOGET <> 0);

INSERT INTO Salesman_Master_11 VALUES ('S00001', 'Aman', 'A/14', 'Worli', 'Mumbai',


400002, 'Maharashtra', 3000, 100, 50, 'Good');

INSERT INTO Salesman_Master_11 VALUES ('S00002', 'Omkar', '65', 'Nariman', 'Mumbai',


400001, 'Maharashtra', 3000, 200, 100, 'Good');
INSERT INTO Salesman_Master_11 VALUES ('S00003', 'Raj', 'P-7', 'Bandra', 'Mumbai', 400032,
'Maharashtra', 3000, 200, 100, 'Good');

INSERT INTO Salesman_Master_11 VALUES ('S00004', 'Ashish', 'A/5', 'Juhu', 'Bombay', 400044,
'Maharashtra', 3500, 200, 150, 'Good');

SELECT * FROM SALESMAN_MASTER_11;

/*salesorder table*/

CREATE TABLE SALES_ORDER(

ORDERNO varchar(6) PRIMARY KEY,

CLIENTNO varchar(6) REFERENCES CLIENT_MASTER,

ORDERDATE date, DELYADDR varchar(25),

SALESMANNO varchar(6)REFERENCES SALESMAN_MASTER,

DELYTYPE char(1) DEFAULT 'F', BILLEDYN char(1),

DELYDATE date, ORDERSTATUS varchar(10),

CONSTRAINT ck_order CHECK (ORDERNO like 'O%'),

CONSTRAINT ck_dely_type CHECK (DELYTYPE IN ('P', 'F')),

CONSTRAINT ck_ord_status

CHECK(ORDERSTATUS IN ('In Process', 'Fulfilled', 'Backorder', 'Cancelled')));

INSERT INTO Sales_Order (OrderNo, OrderDate, ClientNo, DelyType, BilledYn, SalesmanNo,


DelyDate, OrderStatus)

VALUES ('O19001', '2002-06-12', 'C00001', 'F', 'N', 'S00001', '2002-07-20', 'In Process');

INSERT INTO Sales_Order (OrderNo, OrderDate, ClientNo, DelyType, BilledYn, SalesmanNo,


DelyDate, OrderStatus)

VALUES ('O19002', '2002-06-25', 'C00002', 'P', 'N', 'S00002', '2002-07-27', 'Cancelled');

INSERT INTO Sales_Order (OrderNo, OrderDate, ClientNo, DelyType, BilledYn, SalesmanNo,


DelyDate, OrderStatus)
VALUES ('O19003', '2002-02-18', 'C00003', 'F', 'Y', 'S00003', '2002-02-20', 'Fulfilled');

INSERT INTO Sales_Order (OrderNo, OrderDate, ClientNo, DelyType, BilledYn, SalesmanNo,


DelyDate, OrderStatus)

VALUES ('O19004', '2002-04-03', 'C00001', 'F', 'Y', 'S00001', '2002-04-07', 'Fulfilled');

INSERT INTO Sales_Order (OrderNo, OrderDate, ClientNo, DelyType, BilledYn, SalesmanNo,


DelyDate, OrderStatus)

VALUES ('O46866', '2002-05-20', 'C00004', 'P', 'N', 'S00002', '2002-05-22', 'Cancelled');

INSERT INTO Sales_Order (OrderNo, OrderDate, ClientNo, DelyType, BilledYn, SalesmanNo,


DelyDate, OrderStatus)

VALUES ('O19008', '2002-05-24', 'C00005', 'F', 'N', 'S00004', '1996-07-26', 'In Process');

SELECT * FROM SALES_ORDER;

SELECT ClientNo, Name FROM Client_Master

WHERE ClientNo IN(SELECT ClientNo FROM Sales_Order

WHERE OrderNo IN(SELECT OrderNo FROM Sales_Order_Details

WHERE ProductNo IN(SELECT ProductNo FROM Product_Master

WHERE Description = 'Lycra Tops')));

/*salesorder details table*/

CREATE TABLE SALES_ORDER_DETAILS(

ORDERNO varchar(6) REFERENCES SALES_ORDER,

PRODUCTNO varchar(6) REFERENCES PRODUCT_MASTER,

QTYORDERED int, QTYDISP int, PRODUCTRATE decimal(10,2),

PRIMARY KEY (ORDERNO, PRODUCTNO));

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19001', 'P00001', 4, 4, 525);
INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)
VALUES('O19001', 'P07965', 2, 1, 8400);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19001', 'P07885', 2, 1, 5250);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19002', 'P00001', 10, 0, 525);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O46865', 'P07868', 3, 3, 3150);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O46865', 'P07885', 3, 1, 5250);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O46865', 'P00001', 10, 10, 525);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O46865', 'P03453', 4, 4, 1050);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19003', 'P03453', 2, 2, 1050);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19003', 'P06734', 1, 1, 12000);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O46866', 'P07965', 1, 0, 8400);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O46866', 'P07975', 1, 0, 1050);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19008', 'P00001', 10, 5, 525);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19008', 'P07975', 5, 3, 1050);

SELECT * FROM SALES_ORDER_DETAILS;

SELECT SOD.ProductNo, PM.Description

FROM Sales_Order_Details SOD, Sales_Order SO, Product_Master PM,


Client_Master CM

WHERE PM.ProductNo = SOD.ProductNo AND SO.OrderNo = SOD.OrderNo

AND CM.ClientNo = SO.ClientNo AND CM.Name = 'Ivan Bayross';

SELECT SOD.ProductNo, PM.Description, SUM(SOD.QtyOrdered) AS TotalQty

FROM Sales_Order_Details SOD

JOIN Sales_Order SO ON SO.OrderNo = SOD.OrderNo

JOIN Product_Master_121 PM ON PM.ProductNo = SOD.ProductNo

WHERE DATE_FORMAT(SO.DelyDate, '%b-%y') = DATE_FORMAT(NOW(), '%b-%y')

GROUP BY SOD.ProductNo, PM.Description;

SELECT DISTINCT Product_Master_121.ProductNo, Description

FROM Sales_Order_Details, Product_Master_121

WHERE Product_Master_121.ProductNo =Sales_Order_Details.ProductNo;

SELECT DISTINCT Sales_Order.ClientNo, Client_Master.Name

FROM Sales_Order_Details, Sales_Order, Product_Master, Client_Master

WHERE Product_Master.ProductNo = Sales_Order_Details.ProductNo

AND Sales_Order.OrderNo = Sales_Order_Details.OrderNo

AND Client_Master.ClientNo = Sales_Order.ClientNo

AND Description = 'Trousers';

SELECT Sales_Order_Details.ProductNo, Sales_Order_Details.OrderNo


FROM Sales_Order_Details, Sales_Order, Product_Master_121

WHERE Sales_Order.OrderNo = Sales_Order_Details.OrderNo

AND Product_Master_121.ProductNo = Sales_Order_Details.ProductNo

AND Sales_Order_Details.QtyOrdered < 5

AND Product_Master_121.Description = 'Pull Overs';

SELECT SOD.ProductNo, PM.Description, SUM(QtyOrdered) 'Units Ordered'

FROM Sales_Order_Details SOD, Sales_Order SO, Product_Master_121 PM,

Client_Master_11 CM

WHERE SO.OrderNo = SOD.OrderNo AND PM.ProductNo = SOD.ProductNo

AND CM.ClientNo = SO.ClientNo

AND (CM.Name = 'Ivan Bayross' OR CM.Name = 'Mamta Muzumdar')

GROUP BY SOD.ProductNo, PM.Description;

SELECT SO.ClientNo, SOD.ProductNo, PM.Description, SUM(QtyOrdered) 'Units Ordered'

FROM Sales_Order SO, Sales_Order_Details SOD, Product_Master_121 PM,

Client_Master_11 CM

WHERE SO.OrderNo = SOD.OrderNo AND SOD.ProductNo = PM.ProductNo

AND SO.ClientNo = CM.ClientNo

GROUP BY SO.ClientNo, SOD.ProductNo, PM.Description

HAVING SO.ClientNo = 'C00001' OR SO.ClientNo='C00002';

SELECT description, SUM(QtyDisp) FROM Product_Master, Sales_Order_Details


WHERE Product_Master.ProductNo = Sales_Order_Details.ProductNo

GROUP BY Description;

SELECT Sales_Order_Details.ProductNo, Product_Master.Description,

SUM(Sales_Order_Details.QtyDisp * Sales_Order_Details.ProductRate) 'Sales Per

Product'

FROM Sales_Order_Details, Product_Master

WHERE Product_Master.ProductNo = Sales_Order_Details.ProductNo

GROUP BY Sales_Order_Details.ProductNo, Product_Master.Description;

SELECT CM.ClientNo, CM.Name, AVG(SOD.QtyDisp) 'Avg. Sales'

FROM Sales_Order_Details SOD, Sales_Order SO, Client_Master CM

WHERE CM.ClientNo = SO.ClientNo AND SO.OrderNo = SOD.OrderNo

GROUP BY CM.ClientNo, Name

HAVING MAX(SOD.QtyOrdered * SOD.ProductRate) > 15000;

SELECT SO.OrderNo, SO.OrderDate, SUM(SOD.QtyOrdered * SOD.ProductRate) 'Order Billed'

FROM Sales_Order SO, Sales_Order_Details SOD

WHERE SOD.OrderNo = SO.OrderNo AND SO.Billed = 'Y'

AND to_char(OrderDate, 'MON') = 'Jun'

GROUP BY SO.OrderNo;

OUTPUT-

1. Printing the description and total quantity sold for each product.
2. Finding the value of each product sold.

3. Calculating the average quantity sold for each client that has a maximum order value of
15000.00.

4. Finding out the total of all the billed orders for the month of June.
RESULT- The query is successfully implemented.
EXPERIMENT -6

AIM- Write join queries of Client and Product tables.

THEORY- A join in SQL is used to combine rows from two or more tables based on a related
column between them. Joins are fundamental in relational databases as they allow you to
retrieve data spread across multiple tables in a meaningful way. There are several types of
joins, each serving different purposes for how tables are combined.

SOFTWARE USED- MySQL

//CODE

CREATE DATABASE MEMORY;

USE MEMORY;

CREATE TABLE CLIENT_MASTER_11(

CLIENTNO varchar(6) primary key,

NAME varchar(20) not null,

ADDRESS1 varchar(30),

ADDRESS2 varchar(30),

CITY varchar(15),

PINCODE int,

STATE varchar(15),

BALDUE decimal(10,2));

ALTER TABLE Client_Master_11

ADD CONSTRAINT ck_client CHECK (ClientNo LIKE 'C%');

INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue) VALUES
('C00001', 'Ivan Bayross', 'Mumbai', 400054, 'Maharashtra', 15000);

INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue) VALUES
('C00002', 'Mamta Muzumdar', 'Madras', 780001, 'Tamil Nadu', 0);
INSERT INTO Client_Master_11 (ClientNo, Name, City, Pincode, State, BalDue) VALUES
('C00003', 'Chhaya Bankar', 'Mumbai', 400057, 'Maharashtra', 5000);

INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue)VALUES


('C00004', 'Ashwini Joshi', 'Bangalore', 560001, 'Karnataka', 0);

INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue)VALUES


('C00005', 'Hansel Colaco', 'Mumbai', 400060, 'Maharashtra', 2000);

INSERT INTO Client_Master_11 (ClientNo, Name, City, PinCode, State, BalDue) VALUES
('C00006', 'Deepak Sharma', 'Mangalore', 560050, 'Karnataka', 0);

SELECT * FROM CLIENT_MASTER_11;

SELECT Name ,Address1, Address2, City, State, PinCode FROM Client_Master_11 WHERE
ClientNo

IN(SELECT ClientNo FROM Sales_Order WHERE OrderNo = 'O19001');

SELECT ClientNo, Name

FROM Client_Master_11

WHERE ClientNo IN (

SELECT ClientNo

FROM Sales_Order

WHERE DATE_FORMAT(OrderDate, '%b,%y') < 'May,02');

SELECT Name FROM Client_Master_11

WHERE ClientNo IN(SELECT ClientNo FROM Sales_Order

WHERE OrderNo IN(SELECT OrderNo FROM Sales_Order_Details

WHERE (QtyOrdered * ProductRate) >= 10000));


SELECT ClientNo, Name FROM Client_Master_11

WHERE ClientNo IN(SELECT ClientNo FROM Sales_Order

WHERE OrderNo IN(SELECT OrderNo FROM Sales_Order_Details

WHERE ProductNo IN(SELECT ProductNo FROM Product_Master_121

WHERE Description = 'Lycra Tops')));

/* product master table*/

CREATE TABLE PRODUCT_MASTER_121(

PRODUCTNO varchar(6),

DESCRIPTION varchar(15),

PROFITPERCENT decimal(4,2),

UNITMEASURE varchar(10),

QTYONHAND int, REORDERLVL int,

SELLPRICE decimal(8,2), COSTPRICE decimal(8,2));

ALTER TABLE PRODUCT_MASTER_11

ADD CONSTRAINT ck_product CHECK (PRODUCTNO like 'P%'),

ADD CONSTRAINT ck_sell CHECK (SELLPRICE <> 0),

ADD CONSTRAINT ck_cost CHECK (COSTPRICE <> 0);

INSERT INTO Product_Master_121 VALUES ('P00001', 'T-Shirts', 5, 'Piece', 200, 50, 350, 250);

INSERT INTO Product_Master_121 VALUES ('P03453', 'Shirts', 6, 'Piece', 150, 50, 500, 350);

INSERT INTO Product_Master_121 VALUES ('P06734', 'Cotton Jeans', 5, 'Piece', 100, 20, 600,
450);

INSERT INTO Product_Master_121 VALUES ('P07865', 'Jeans', 5, 'Piece', 100, 20, 750, 500);

INSERT INTO Product_Master_121 VALUES ('P07868', 'Trousers', 2, 'Piece', 150, 50, 850, 550);
INSERT INTO Product_Master_121 VALUES ('P07885', 'Pull Overs', 2.5, 'Piece', 80, 30, 700, 450);

INSERT INTO Product_Master_121 VALUES ('P07965', 'Denim Shirts', 4, 'Piece', 100, 40, 350,
250);

INSERT INTO Product_Master_121 VALUES ('P07975', 'Lycra Tops', 5, 'Piece', 70, 30, 300, 175);

INSERT INTO Product_Master_121 VALUES ('P08865', 'Skirts', 5, 'Piece', 75, 30, 450, 300);

SELECT * FROM PRODUCT_MASTER_121;

SELECT ProductNo, Description FROM Product_Master_11 WHERE ProductNo

NOT IN(SELECT ProductNo FROM Sales_Order_Details);

/*salesman table*/

CREATE TABLE SALESMAN_MASTER_11(

SALESMANNO varchar(6), SALESMANNAME varchar(20),

ADDRESS1 varchar(30), ADDRESS2 varchar(30),

CITY varchar(20), PINCODE int,

STATE varchar(20), SALAMT decimal(8,2),

TGTTOGET decimal(6,2), YTDSALES decimal(6,2),

REMARKS varchar(60));

ALTER TABLE SALESMAN_MASTER_11

ADD CONSTRAINT ck_salesman CHECK (SALESMANNO like 'S%'),

ADD CONSTRAINT ck_sal CHECK (SALAMT <> 0),

ADD CONSTRAINT ck_target CHECK (TGTTOGET <> 0);

INSERT INTO Salesman_Master_11 VALUES ('S00001', 'Aman', 'A/14', 'Worli', 'Mumbai',


400002, 'Maharashtra', 3000, 100, 50, 'Good');

INSERT INTO Salesman_Master_11 VALUES ('S00002', 'Omkar', '65', 'Nariman', 'Mumbai',


400001, 'Maharashtra', 3000, 200, 100, 'Good');
INSERT INTO Salesman_Master_11 VALUES ('S00003', 'Raj', 'P-7', 'Bandra', 'Mumbai', 400032,
'Maharashtra', 3000, 200, 100, 'Good');

INSERT INTO Salesman_Master_11 VALUES ('S00004', 'Ashish', 'A/5', 'Juhu', 'Bombay', 400044,
'Maharashtra', 3500, 200, 150, 'Good');

SELECT * FROM SALESMAN_MASTER_11;

/*salesorder table*/

CREATE TABLE SALES_ORDER(

ORDERNO varchar(6) PRIMARY KEY,

CLIENTNO varchar(6) REFERENCES CLIENT_MASTER,

ORDERDATE date, DELYADDR varchar(25),

SALESMANNO varchar(6)REFERENCES SALESMAN_MASTER,

DELYTYPE char(1) DEFAULT 'F', BILLEDYN char(1),

DELYDATE date, ORDERSTATUS varchar(10),

CONSTRAINT ck_order CHECK (ORDERNO like 'O%'),

CONSTRAINT ck_dely_type CHECK (DELYTYPE IN ('P', 'F')),

CONSTRAINT ck_ord_status

CHECK(ORDERSTATUS IN ('In Process', 'Fulfilled', 'Backorder', 'Cancelled')));

INSERT INTO Sales_Order (OrderNo, OrderDate, ClientNo, DelyType, BilledYn, SalesmanNo,


DelyDate, OrderStatus)

VALUES ('O19001', '2002-06-12', 'C00001', 'F', 'N', 'S00001', '2002-07-20', 'In Process');

INSERT INTO Sales_Order (OrderNo, OrderDate, ClientNo, DelyType, BilledYn, SalesmanNo,


DelyDate, OrderStatus)

VALUES ('O19002', '2002-06-25', 'C00002', 'P', 'N', 'S00002', '2002-07-27', 'Cancelled');

INSERT INTO Sales_Order (OrderNo, OrderDate, ClientNo, DelyType, BilledYn, SalesmanNo,


DelyDate, OrderStatus)
VALUES ('O19003', '2002-02-18', 'C00003', 'F', 'Y', 'S00003', '2002-02-20', 'Fulfilled');

INSERT INTO Sales_Order (OrderNo, OrderDate, ClientNo, DelyType, BilledYn, SalesmanNo,


DelyDate, OrderStatus)

VALUES ('O19004', '2002-04-03', 'C00001', 'F', 'Y', 'S00001', '2002-04-07', 'Fulfilled');

INSERT INTO Sales_Order (OrderNo, OrderDate, ClientNo, DelyType, BilledYn, SalesmanNo,


DelyDate, OrderStatus)

VALUES ('O46866', '2002-05-20', 'C00004', 'P', 'N', 'S00002', '2002-05-22', 'Cancelled');

INSERT INTO Sales_Order (OrderNo, OrderDate, ClientNo, DelyType, BilledYn, SalesmanNo,


DelyDate, OrderStatus)

VALUES ('O19008', '2002-05-24', 'C00005', 'F', 'N', 'S00004', '1996-07-26', 'In Process');

SELECT * FROM SALES_ORDER;

SELECT ClientNo, Name FROM Client_Master

WHERE ClientNo IN(SELECT ClientNo FROM Sales_Order

WHERE OrderNo IN(SELECT OrderNo FROM Sales_Order_Details

WHERE ProductNo IN(SELECT ProductNo FROM Product_Master

WHERE Description = 'Lycra Tops')));

/*salesorder details table*/

CREATE TABLE SALES_ORDER_DETAILS(

ORDERNO varchar(6) REFERENCES SALES_ORDER,

PRODUCTNO varchar(6) REFERENCES PRODUCT_MASTER,

QTYORDERED int, QTYDISP int, PRODUCTRATE decimal(10,2),

PRIMARY KEY (ORDERNO, PRODUCTNO));

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19001', 'P00001', 4, 4, 525);
INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)
VALUES('O19001', 'P07965', 2, 1, 8400);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19001', 'P07885', 2, 1, 5250);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19002', 'P00001', 10, 0, 525);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O46865', 'P07868', 3, 3, 3150);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O46865', 'P07885', 3, 1, 5250);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O46865', 'P00001', 10, 10, 525);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O46865', 'P03453', 4, 4, 1050);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19003', 'P03453', 2, 2, 1050);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19003', 'P06734', 1, 1, 12000);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O46866', 'P07965', 1, 0, 8400);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O46866', 'P07975', 1, 0, 1050);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19008', 'P00001', 10, 5, 525);

INSERT INTO Sales_Order_Details (OrderNo, ProductNo, QtyOrdered, QtyDisp, ProductRate)


VALUES('O19008', 'P07975', 5, 3, 1050);

SELECT * FROM SALES_ORDER_DETAILS;

SELECT SOD.ProductNo, PM.Description

FROM Sales_Order_Details SOD, Sales_Order SO, Product_Master PM,


Client_Master CM

WHERE PM.ProductNo = SOD.ProductNo AND SO.OrderNo = SOD.OrderNo

AND CM.ClientNo = SO.ClientNo AND CM.Name = 'Ivan Bayross';

SELECT SOD.ProductNo, PM.Description, SUM(SOD.QtyOrdered) AS TotalQty

FROM Sales_Order_Details SOD

JOIN Sales_Order SO ON SO.OrderNo = SOD.OrderNo

JOIN Product_Master_121 PM ON PM.ProductNo = SOD.ProductNo

WHERE DATE_FORMAT(SO.DelyDate, '%b-%y') = DATE_FORMAT(NOW(), '%b-%y')

GROUP BY SOD.ProductNo, PM.Description;

SELECT DISTINCT Product_Master_121.ProductNo, Description

FROM Sales_Order_Details, Product_Master_121

WHERE Product_Master_121.ProductNo =Sales_Order_Details.ProductNo;

SELECT DISTINCT Sales_Order.ClientNo, Client_Master.Name

FROM Sales_Order_Details, Sales_Order, Product_Master, Client_Master

WHERE Product_Master.ProductNo = Sales_Order_Details.ProductNo

AND Sales_Order.OrderNo = Sales_Order_Details.OrderNo

AND Client_Master.ClientNo = Sales_Order.ClientNo

AND Description = 'Trousers';

SELECT Sales_Order_Details.ProductNo, Sales_Order_Details.OrderNo


FROM Sales_Order_Details, Sales_Order, Product_Master_121

WHERE Sales_Order.OrderNo = Sales_Order_Details.OrderNo

AND Product_Master_121.ProductNo = Sales_Order_Details.ProductNo

AND Sales_Order_Details.QtyOrdered < 5

AND Product_Master_121.Description = 'Pull Overs';

SELECT SOD.ProductNo, PM.Description, SUM(QtyOrdered) 'Units Ordered'

FROM Sales_Order_Details SOD, Sales_Order SO, Product_Master_121 PM,

Client_Master_11 CM

WHERE SO.OrderNo = SOD.OrderNo AND PM.ProductNo = SOD.ProductNo

AND CM.ClientNo = SO.ClientNo

AND (CM.Name = 'Ivan Bayross' OR CM.Name = 'Mamta Muzumdar')

GROUP BY SOD.ProductNo, PM.Description;

SELECT SO.ClientNo, SOD.ProductNo, PM.Description, SUM(QtyOrdered) 'Units Ordered'

FROM Sales_Order SO, Sales_Order_Details SOD, Product_Master_121 PM,

Client_Master_11 CM

WHERE SO.OrderNo = SOD.OrderNo AND SOD.ProductNo = PM.ProductNo

AND SO.ClientNo = CM.ClientNo

GROUP BY SO.ClientNo, SOD.ProductNo, PM.Description

HAVING SO.ClientNo = 'C00001' OR SO.ClientNo='C00002';

SELECT description, SUM(QtyDisp) FROM Product_Master, Sales_Order_Details


WHERE Product_Master.ProductNo = Sales_Order_Details.ProductNo

GROUP BY Description;

SELECT Sales_Order_Details.ProductNo, Product_Master.Description,

SUM(Sales_Order_Details.QtyDisp * Sales_Order_Details.ProductRate) 'Sales Per

Product'

FROM Sales_Order_Details, Product_Master

WHERE Product_Master.ProductNo = Sales_Order_Details.ProductNo

GROUP BY Sales_Order_Details.ProductNo, Product_Master.Description;

SELECT CM.ClientNo, CM.Name, AVG(SOD.QtyDisp) 'Avg. Sales'

FROM Sales_Order_Details SOD, Sales_Order SO, Client_Master CM

WHERE CM.ClientNo = SO.ClientNo AND SO.OrderNo = SOD.OrderNo

GROUP BY CM.ClientNo, Name

HAVING MAX(SOD.QtyOrdered * SOD.ProductRate) > 15000;

SELECT SO.OrderNo, SO.OrderDate, SUM(SOD.QtyOrdered * SOD.ProductRate) 'Order Billed'

FROM Sales_Order SO, Sales_Order_Details SOD

WHERE SOD.OrderNo = SO.OrderNo AND SO.Billed = 'Y'

AND to_char(OrderDate, 'MON') = 'Jun'

GROUP BY SO.OrderNo;

OUTPUT-

1. Find out the products, which have been sold to 'Ivan Bayross'.
2. Listing the ProductNo and description of constantly sold (i.e. rapidly moving) products.

3. Finding the names of clients who have purchased 'Trousers'.

4. Listing the products and orders from customers who have ordered less than 5 units of 'Pull
Overs'.
5. Finding the products and their quantities for the orders placed by 'Ivan Bayross' and 'Mamta
Muzumdar'.

6. Finding the products and their quantities for the orders placed by ClientNo 'C00001' and
'C00002'.

RESULT- Join has been studied and implemented successfully.


EXPERIMENT- 10

AIM- Create the tables EMPLOYEE, WORKS, COMPANY, MANAGES. Specify primary and foreign
keys. Enter at least 5 tuples in each table with relevant data and solve the given queries.

THEORY- A primary key is used to ensure that data in the specific column is unique. A column
cannot have NULL values. It is either an existing table column or a column that is specifically
generated by the database according to a defined sequence.

A foreign key is a column or group of columns in a relational database table that provides a
link between data in two tables. It is a column (or columns) that references a column (most
often the primary key) of another table.

SOFTWARE USED- MySQL

//CODE

create database dbmsfile1;

use dbmsfile1;

create table employee (

employeename varchar(20) primary key,

street varchar(20),

city varchar(20));

insert into employee values ("Neha","A street","A city"),

("Reesha","B street","B city"),

("Ritika","C street","C city"),

("Ritu","C street","C city"),

("Ryan","A street","A city"),

("Kelly","B street","B city");

create table company1(


companyname varchar(50) primary key,

city varchar(50));

insert into company1 values ("First Bank Corporation","A city"),

("Small Bank Corporation","B city"),

("No Bank Corporation","C city"),

("Yes Bank Corporation","A city"),

("More Bank Corporation","B city");

create table works12(

employeename varchar(100),

companyname varchar(100),

salary double,

primary key (employeename,companyname),

foreign key (employeename) references employee(employeename),

foreign key (companyname) references company1(companyname));

insert into works12 values ("Neha","First Bank Corporation",40000),

("Reesha","Small Bank Corporation",30000),

("Ritika","No Bank Corporation",35000),

("Ritu","Small Bank Corporation",25000),

("Ryan","First Bank Corporation",15000),

("Kelly","First Bank Corporation",10000);

create table manages(


employeename varchar(20),

managername varchar(20),

primary key (employeename,managername),

foreign key (employeename) references employee(employeename),

foreign key (managername) references employee(employeename));

insert into manages values ("Neha","Ryan"),("Neha","Kelly"),("Reesha","Ritu");

select "Table display";

select * from employee e

join (works12 natural join company)

on e.employeename=e.employeename

order by companyname;

select "--------------------------------------------------";

select e.employeename,e.street,e.city

from employee e, works12 w

where e.employeename=w.employeename

and companyname="First Bank Corporation"

and salary>10000;
select e.employeename

from employee e,works12 w, company1 c

where e.employeename=w.employeename

and w.companyname=c.companyname

and e.city=c.city;

select avg(salary) as "Average Salary"

from works12

group by companyname;

select e.employeename

from employee e,employee m, manages r

where e.employeename=r.employeename

and m.employeename=r.managername

and e.city=m.city

and e.street=m.street;
select employeename

from works12

where companyname<>"First Bank Corporation";

select employeename

from works12

where salary>

(select max(salary)

from works12

where companyname="Small Bank Corporation");

select e.employeename,e.street,e.city

from employee e, works12 w

where e.employeename=w.employeename

and companyname="First Bank Corporation"

and salary between 10000 and 20000;


select e.employeename

from employee e, works12 w, company1 c

where e.employeename=w.employeename

and w.companyname=c.companyname

and e.city=c.city;

select employeename

from works12

where salary>

(select max(salary)

from works12

where companyname="Small Bank Corporation");

RESULT- The query has been implemented successfully.

You might also like