0% found this document useful (0 votes)
35 views11 pages

SQL Table Creation and Queries Guide

The document outlines the creation of database tables for a client, product, salesman, sales order, and order details, including their respective attributes and constraints. It also includes SQL insert statements for populating these tables with sample data and various queries to retrieve specific information from the database. Additionally, it presents a second assignment involving employee and company tables with similar structures and queries.

Uploaded by

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

SQL Table Creation and Queries Guide

The document outlines the creation of database tables for a client, product, salesman, sales order, and order details, including their respective attributes and constraints. It also includes SQL insert statements for populating these tables with sample data and various queries to retrieve specific information from the database. Additionally, it presents a second assignment involving employee and company tables with similar structures and queries.

Uploaded by

suchismitabose29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ASSIGNMENT: 6.

CREATE TABLES
CREATE TABLE CLIENT(Client_No VARCHAR2(6) CHECK(Client_No LIKE 'C%') PRIMARY
KEY, Name VARCHAR2(20) NOT NULL, City VARCHAR(15), Pincode NUMBER(8), State
VARCHAR2(15), BalDue NUMBER(10,2));

CREATE TABLE PRODUCT(Product_No VARCHAR2(6) CHECK(Product_No LIKE 'P%')


PRIMARY KEY, Description VARCHAR2(15) NOT NULL, QTY_ON_HAND NUMBER(8) NOT
NULL, Sell_Price NUMBER(8,2) CHECK(Sell_Price <>0) NOT NULL, Cost_Price
NUMBER(8,2) CHECK(Cost_Price <>0) NOT NULL);

CREATE TABLE SALESMAN(Salesman_No VARCHAR2(6) CHECK(Salesman_No LIKE 'S%')


PRIMARY KEY, Salesman_Name VARCHAR2(20) NOT NULL, City VARCHAR2(20) NOT
NULL, Pincode NUMBER(8) NOT NULL, State VARCHAR2(20) NOT NULL, Sal_Amt
NUMBER(8,2) CHECK(Sal_Amt <>0) NOT NULL);

CREATE TABLE SALESORDER(Order_No VARCHAR2(6) CHECK(Order_No LIKE 'O%')


PRIMARY KEY, Client_No VARCHAR2(6) REFERENCES CLIENT(Client_No) ON DELETE
CASCADE, Order_Date DATE, Salesman_No VARCHAR2(6) REFERENCES
SALESMAN(Salesman_No), Dely_Type CHAR(1) DEFAULT 'F' CHECK(Dely_type= 'P' OR
Dely_Type= 'F'), Dely_Date DATE);

CREATE TABLE ORDER_DETAILS(Order_No VARCHAR2(6) REFERENCES


SALESORDER(Order_No) ON DELETE CASCADE, Product_No VARCHAR2(6)
REFERENCES PRODUCT(Product_No), Qty_disp NUMBER(8), Product_Rate NUMBER(10,2));

INSERT VALUES
CLIENT

INSERT INTO CLIENT VALUES('C01', 'Robb', 'Kolkata', 700091, 'West Bengal', 200);
INSERT INTO CLIENT VALUES('C02', 'Sansa', 'Patna', 700021, 'Bihar', 400);
INSERT INTO CLIENT VALUES('C03', 'Arya', 'Mumbai', 700051, 'Maharashtra', 500);
INSERT INTO CLIENT VALUES('C04', 'Bran', 'Tamil Nadu', 700891, 'Chennai', 300);
INSERT INTO CLIENT VALUES('C05', 'Rickon', 'Kolkata', 7000981, 'West Bengal', 100);
PRODUCT

Insert into PRODUCT Values ('P01', 'Dress', 20, 1000, 700);


Insert into PRODUCT Values ('P02', 'Eyeliner', 40, 100, 200);
Insert into PRODUCT Values ('P03', 'Laptop', 10, 35000, 50000);
Insert into PRODUCT Values ('P04', 'Sipper', 30, 100, 200);
Insert into PRODUCT Values ('P05', 'Cream', 55, 100, 150);

SALESMAN

Insert into Salesman Values ('S01', 'Jon', 'Kolkata', 700026, 'West Bengal', 10000);
Insert into Salesman Values ('S02', 'Dany', 'Mumbai', 700026, 'Maharashtra', 20000);
Insert into Salesman Values ('S03', 'Tyrion', 'Kolkata', 700026, 'West Bengal', 35000);
Insert into Salesman Values ('S04', 'Jaime', 'Ranchi', 700026, 'Jharkhand', 46000);
Insert into Salesman Values ('S05', 'Cersei', 'Guwahati', 700026, 'Assam', 68000);
SALESORDER

Insert into Salesorder Values ('O01', 'C01', To_date('2023-11-12', 'YYYY-MM-DD'), 'S01', 'F',
To_date('2023-12-25', 'YYYY-MM-DD'));
Insert into Salesorder Values ('O02', 'C02', To_date('2023-10-12', 'YYYY-MM-DD'), 'S02', 'P',
To_date('2023-11-25', 'YYYY-MM-DD'));
Insert into Salesorder Values ('O03', 'C03', To_date('2023-09-12', 'YYYY-MM-DD'), 'S03', 'F',
To_date('2023-10-25', 'YYYY-MM-DD'));
Insert into Salesorder Values ('O04', 'C04', To_date('2023-08-12', 'YYYY-MM-DD'), 'S04', 'P',
To_date('2023-09-25', 'YYYY-MM-DD'));
Insert into Salesorder Values ('O05', 'C05', To_date('2023-07-12', 'YYYY-MM-DD'), 'S05', 'P',
To_date('2023-08-25', 'YYYY-MM-DD'));

ORDER_DETAILS

Insert into Order_Details Values ('O01', 'P01', 60, 100.89);


Insert into Order_Details Values ('O02', 'P02', 70, 90.89);
Insert into Order_Details Values ('O03', 'P03', 80, 80.89);
Insert into Order_Details Values ('O04', 'P04', 90, 70.89);
Insert into Order_Details Values ('O05', 'P05', 10, 60.89);
QUERIES
1. List the names of all clients having ‘a’ as the last letter in their names.

Select Name from Client where Name like '%a' ;

2. List the clients who stay in a city whose first letter is ‘K’.

Select Name, City from Client where City like 'K%' ;

3. List all the clients who stay in ‘Mumbai’ or ‘Kolkata’.

Select Name, City from Client where City in ('Mumbai', 'Kolkata') ;

4. List all the clients whose BalDue is greater than value 100.

Select Name, BalDue from Client where BalDue > 100 ;


5. List all information from the Salesorder table for orders placed in the month of July.

Select * from Salesorder where Extract (Month from Order_Date) = 07;

6. List the order information for Client_no ‘C01’ and ‘C03’.

Select * from Salesorder where Client_no in ('C01', 'C03') ;

7. List products whose selling price is greater than 500 and less than or equal to 1000.

Select * from Product where Sell_Price > 500 and Sell_Price <= 1000 ;

8. Count the total number of order.

Select Count(*) as Order_no from Salesorder ;

9. Determine the maximum and minimum product prices. Rename the output as max_price
and min_price respectively.

Select Max(Sell_Price) as Max_Price, Min(Sell_Price) as Min_Price from Product;


10. Count the number of client who live in Kolkata.

Select Count(*) as Client_Kol from Client where City = 'Kolkata' ;

11. Count the number of products having price less than or equal to 500.

Select Count(*) as Product_No from Product where Sell_Price <= 500 ;

12. List the order number and day on which clients placed their order.

Select Order_No, Order_Date from Salesorder ;

13. List the Order_Date in the format ‘DD-Month-YY’.

Select To_Char(Order_Date, 'DD-Month-YY') as formatted_order_date from Salesorder ;

14. List the date, 20 days after today’s date.

Select Sysdate + 20 as Date_after_20 from Dual ;


15. List name of the client who has maximum BalDue.

Select Name from Client


Order by BalDue Desc
Fetch first 1 row only ;

16. Find the difference between maximum BalDue and minimum BalDue.

Select Max(BalDue) - Min(BalDue) as Diff from Client ;

17. Add Rs.1000/- with the salary amount of every salesmen.

Update Salesman Set Sal_Amt = Sal_Amt + 1000 ;


Select Salesman_Name, Sal_Amt from Salesman ;
ASSIGNMENT: 6.2

CREATE TABLES
Create table EMPLOYEE (EMP_NO Varchar2 (6) check (EMP_NO like 'E%') Primary Key,
NAME Varchar2 (20) not null, DOB Date, SEX Char (1) default 'F' check (SEX= 'M' or SEX= 'F'),
ADDRESS Varchar2 (30), SALARY Number (10,2)) ;

Create table COMPANY (COMP_NO Varchar2 (6) check (COMP_NO like 'C%') Primary Key,
NAME Varchar2 (20) not null, ADDRESS Varchar2 (30)) ;

Create table WORKS (EMP_NO Varchar2(6) references EMPLOYEE (EMP_NO) on delete


cascade, COMP_NO Varchar2(6) references COMPANY (COMP_NO) on delete cascade) ;

INSERT VALUES
EMPLOYEE

Insert into EMPLOYEE values ('E00001', 'Bella', To_date ('2013-07-12', 'YYYY-MM-DD'), 'F',
'Camel Street, Ohio', 25000) ;
Insert into EMPLOYEE values ('E00002', 'Diana', To_date ('1989-05-14', 'YYYY-MM-DD'), 'F',
'Camel Street, Ohio', 40000) ;
Insert into EMPLOYEE values ('E00003', 'Ken', To_date ('2003-12-22', 'YYYY-MM-DD'), 'M',
'Dempstrey Lane, New Jersey', 70000) ;
Insert into EMPLOYEE values ('E00004', 'Agatha', To_date ('1990-09-12', 'YYYY-MM-DD'), 'F',
'Camel Street, Ohio', 35000) ;
Insert into EMPLOYEE values ('E00005', 'Roger', To_date ('1999-07-12', 'YYYY-MM-DD'), 'M',
'Butterfly Street, Ohio', 12000) ;
COMPANY

Insert into COMPANY values ('C00001', 'Clifford Corp', 'New York') ;


Insert into COMPANY values ('C00002', 'Google', 'Houston') ;
Insert into COMPANY values ('C00003', 'Facebook', 'New York') ;
Insert into COMPANY values ('C00004', 'Microsoft', 'New Orleans') ;
Insert into COMPANY values ('C00005', 'Apple', 'Ohio') ;

WORKS

Insert into WORKS values ('E00001', 'C00002') ;


Insert into WORKS values ('E00002', 'C00001') ;
Insert into WORKS values ('E00003', 'C00001') ;
Insert into WORKS values ('E00001', 'C00003') ;
Insert into WORKS values ('E00004', 'C00005') ;

QUERIES

1. List the employees who work for company ‘C00002’.

Select * from EMPLOYEE e join WORKS w on e.Emp_No = w.Emp_No where


w.Comp_No = 'C00002' ;
2. List the employees who work for company ‘C00005’.

Select * from EMPLOYEE e join WORKS w on e.Emp_No = w.Emp_No where


w.Comp_No = 'C00005' ;

3. List the employees who work for Clifford Corp.

Select * from EMPLOYEE e join WORKS w on e.Emp_No = w.Emp_No join COMPANY c


on c.Comp_No = w.Comp_No where [Link] = 'Clifford Corp' ;

4. List the employees whose name ends with ‘a’.

Select * from EMPLOYEE where Name like '%a' ;


5. List the employees born between 1999 and 2011.

Select * from EMPLOYEE where DOB between To_Date ('1991-01-01', 'YYYY-MM-DD') and
To_Date ('2011-01-01', 'YYYY-MM-DD') ;

You might also like