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

SQ L Examples

The document describes the creation of multiple databases and tables across different databases using SQL commands. It creates databases called Employee, Student, Customer and Sales. It then creates various tables within each database to store employee details, student details, customer details, sales details, unit sales data, bills and representative details. Records are inserted into each table using INSERT statements.

Uploaded by

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

SQ L Examples

The document describes the creation of multiple databases and tables across different databases using SQL commands. It creates databases called Employee, Student, Customer and Sales. It then creates various tables within each database to store employee details, student details, customer details, sales details, unit sales data, bills and representative details. Records are inserted into each table using INSERT statements.

Uploaded by

YUVRAJ WAGH
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Create a database called EMPLOYEE and create the

following table in it:

CREATE DATABASE Employee;


USE Employee;
Table name : Employee details(EMP_DETAILS)
column s in the table: Employee no.(Primary key) first name,
Surname, middle name, age, date of birth, gender, name of the
department, designation and salary
CREATE TABLE EMP_DETAILS
(emp_no INT PRIMARY KEY NOT NULL,
fname CHAR(15),
surname CHAR(15),
mname CHAR(15),
age INT,
dob DATE,
Gender ENUM('m','f'),
dept VARCHAR(25),
desig VARCHAR(25),
sal DECIMAL(7,2));
INSERT INTO EMP_DETAILS VALUES
(101,"SAGAR","SHAH","MOHAN",47,'1962-04-
12',"M","ACCOUNTS","ACCOUNTANT",9543.50),
(102,"VIJAYA","DESAI","VINAY",31 ,'1971-12-
15',"F","PRODUCTION","MANAGER",3500.00),
(103,"PRIYANKA"," SHETTY","DIWAKAR",24,'1985-11-
02' ,"F","PRODUCTION","SUBORDINATE",14550.00),
(104,"NEHA","KOTAIN","SIROSH",56 ,'1954-08-
19',"F","SALES","MANAGER",15000.50),
(105,"SIRAJ","KHAN","ZEESHAN",29 ,'1980-06-
22',"M","ACCOUNTS","CASHIER",9590.50),
(106,"NISHITA","SHETTY","MADHUKAR", 54,'1956-12-
30',"F","MARKETING","EXECUTIVE",16403.50),
(107,"SHRIYA ","SHARMA","DILIP",30,'1979-04-
13' ,"F","MARKETING","EXECUTIVE",8755.00),
(108,"KARAN","GORE","PRAKASH",45,'1964-03-
25' ,"M","ACCOUNTS","ACCOUNTANT",15405.50),
(109,"PRAFUL","MEHTA","NARESH",37,'1972-01-
09',"M","MARKETING","EXECUTIVE",2570.50),
(110,"NITIN"," RAJPURE","GOVIND",36 ,'1973-05-
06',"M","FINANCE","MANAGER",20400.50);
Table name : employee personal detail(EMP_PERSONAL).
Columns in the table: Employee no.(Primary key),residential
address, phone number, mobile number,
Marital status,spouse name(if married),no. of children, email
address, appointment date.

CREATE TABLE EMP_PERSONAL


(emp_no INT PRIMARY KEY NOT NULL,
addr VARCHAR(10),
ph_no CHAR(10),
mobile CHAR(10),
marital_status ENUM('Y','N'),
spouse VARCHAR(5),
no_of_child INT,
email VARCHAR(15),
app_date DATE);
INSERT INTO EMP_PERSONAL
VALUES
(101,"CHEMBUR",'15785345','9822656587','N',NULL,NULL,"FLYSIT@NOWHERE.
COM",'2009-05-12'),
(102,"BANDRA",'45752315','9819461026','Y',"SAM",1,"[email protected]
OM",'2009-05-05'),
(103,"CHEMBUR",'4562875','9821335127','N',NULL,NULL,"HIGHLOW@EVERYW
HERE.COM",'2009-04-28'),
(104,"MULUND",'25874545','9822662542','Y',"MOHAN",NULL,"[email protected]
",'2009-04-29'),
(105,"KURLA",'24867645','9821300000','N',NULL,NULL,"[email protected]
OM",'2009-05-12'),
(106,"MULUND",'45898531','9822625642','Y',"NIKHIL",2,"[email protected]",'20
09-04-12'),
(107,"DADAR",'54687512','9821300012','Y',"DINESH",2,"[email protected]
",'2009-05-19'),
(108,"DADAR",'246875132','9822625122','N',NULL,NULL,"WDWCDDIOU@GVGV.
COM",'2009-05-06'),
(109,"DOMBIVLI",'46721537','9821304212','Y',"VRUSHALI",1,"[email protected]
OM",'2009-04-22'),
(110,"THANE",'457878645','9821304256','Y',"NIRJARA",2,"[email protected]
M",'2009-05-02')
Create a database called student and create the following
tables in it.

CREATE DATABASE student;


USE student;
Table name: TYBCOM _STUDENTS
Columns in the table: Admission number (Primary key),first
name, surname, last name, date of birth, marks in HSC,
gender,date of birth(Admission no. should be automatically
generated every time you enter a record)

CREATE TABLE TYBCOM_STUDENTS


(adm_no INT AUTO_INCREMENT PRIMARY KEY,
fname CHAR(15),
surname CHAR(15),
lname CHAR(15),
dob DATE,
mks_HSC INT,
gender ENUM('m','f'));
Records:TYBCOM_STUDENTS
INSERT INTO Tybcom_students VALUES
(1251,"sagar","shah","Mohan",'1991-04-12',62,"M"),
(null,"Vijaya","Desai","Vinay",'1991-12-15',57,"F"),
(null,"Priyanka"," Shetty","Diwakar",'1992-11-02',82 ,"F"),
(null,"Neha","Kotain","Sirosh",'1991-08-19',71,"F"),
(null,"Siraj","Khan","Zeeshan",'1990-06-22',75,"M"),
(null,"Nishita","Shetty","Madhukar", '1992-12-30',79,"F"),
(null,"Shriya ","Sharma","Dilip",'1992-04-13',91 ,"F"),
(null,"Karan","Gore","Prakash",'1991-03-25' ,69,"M"),
(null,"Praful","Mehta","Naresh",'1992-01-09',83,"M"),
(null,"Nitin"," Rajpure","Govind",'1991-05-06',64,"M");
b)Table name:Exam1
Columns in the table: : Admission number (Primary
key),Roll no, and marks in accounts,maths,eco,bc,fc
and commerce1

CREATE TABLE Exam


(adm_no INT AUTO_INCREMENT PRIMARY KEY,
rollno SMALLINT(3),
acts SMALLINT,
mths SMALLINT,
eco SMALLINT,
bc SMALLINT,
fc SMALLINT,
comm SMALLINT);
Records: EXAM1
INSERT INTO EXAM VALUES
(1251,29,41,46,21,40,36,33),
(null,301,35,36,32,30,29,23),
(null,421,21,33,25,29,29,39),
(null,59,35,28,24,35,26,26),
(null,98,23,46,35,41,25,30),
(null,235,33,26,23,35,19,20),
(null,809,35,31,26,16,25,15),
(null,508,18,36,32,20,19,23),
(null,756,43,42,22,29,25,36),
(null,561,23,27,34,19,20,44);
c) Table name : Library
Columns in the table: : Admission number,Book name,
accession no, date of issue, authors name.
(Add 10 records to each of the above tables Using INSERT
INTO command.)

CREATE TABLE Library


(adm_no INT AUTO_INCREMENT PRIMARY KEY,
Bookname CHAR(50),
acc_no INT,
dtofissue DATE,
author CHAR(20));
Records: LIBRARY
INSERT INTO LIBRARY VALUES
(1251,"FREEDOM ",1002,'2009-12-30',"J.KRISHNAMURTI"),
(null,"ACCOUNTANCY",506,'2010-01-19',"CHOUDHARI CHOPDE"),
(null,"FOUNDATION COURSE",231,'2010-01-20',"BASANTANI"),
(null,"MONK WHO SOLD HIS FERARI",459,'2010-01-09',"ROBIN
SHARMA"),
(null,"MATHEMATICS TECHNIQUES",198,'2010-01-15',"DOKE
DESHPANDE"),
(null,"ACCOUNTANCY",235,'2009-12-28',"AINAPURE"),
(null,"BUSINESS COMMUNICATION",809,'2010-01-14',"SATYENDRA
GAUR"),
(null,"ECONOMICS",508,'2010-01-25',"USHA IYER"),
(null,"HARRY POTTER ",756,'2009-11-16',"J.K.ROWLING"),
(null,"COMMERCE",561,'2010-01-19',"MICHAEL VAZ ");
3) Create a database called Customer and create
the following tables in it.

CREATE DATABASE customer;


USE customer;
Table name :Sales details
Customer id, Name,type of customer(Local,outstation),
order no.,Item name, quantity ordered,unit price, date
of order,advance received.
CREATE TABLE sales_details
(Cid INT PRIMARY KEY NOT NULL,
Cname VARCHAR(13),
C_Type ENUM('Local','Outstation'),
orderno INT,
item_name CHAR(20),
qty_order INT,
unit_price INT,
date_of_ordr DATE,
adv INT);
Records: SALES_DETAILS
INSERT INTO sales_details VALUES
(2301,"Roshan Singh", 'Outstation', 2356887,"Garnier Conditioner", 125,
55,'2010-01-19',3500),
(2302,"Druv Mehta", 'Local', 8875777,"Bikaji Sweets", 200, 75,'2010-01-
12',10000),
(2303,"Shashi Bag”,’Outstation', 5687893,"Lakme Compact", 50, 99,'2010-02-
01',2000),
(2304,"Farhan Khan",'Local', 1298756,"Colgate", 150, 50,'2010-01-19',5000),
(2305,"Shriram Nene",'Local', 2754511,"Lakme Compact",75,99,'2010-01-
10',6500),
(2306,"Arvind Gore",'Local', 6547856,"Garnier Conditioner",150,55,'2010-01-
31',5000),
(2307,"Harish Totle",'Outstation', 7895621,"Garnier Conditioner",250,55,'2010-
01-19',9000),
(2308,"Sokhe Tease",'Outstation', 5547786,"Dettol Soap",250,25,'2010-01-
11',4500),
(2309,"Girish Rao",'Local', 4568790,"Lakme Moisturiser",100,60,'2010-01-
27',3000),
(2310,"Seema Apte",'Outstation', 5689745,"Bikaji Sweets",250,75,'2010-01-
19',13000);
Table name: Customer details
customer id (Primary key and unique),address,
phone numbers, contact person, email id.
(Add 10 records to each of the above tables Using
INSERT INTO command.)

CREATE TABLE customer_details


(Cid INT UNIQUE PRIMARY KEY NOT NULL,
addr VARCHAR(30),
phone CHAR(15),
contact_person CHAR(20),
email_id VARCHAR(50));
Create a database called Sales and create the
following tables in it.

CREATE DATABASE sales;


USE sales;
Table name: UNIT_SALES
Salesman Id., Product name, no. of units to be supplied, date of
supply,store no.
CREATE TABLE unit_sales
(slm_id INT PRIMARY KEY NOT NULL,
Prodname VARCHAR(30),
Qty_supply INT(5),
date_of_supply DATE,
store_no INT(10));
Records:UNIT_ SALES
INSERT INTO unit_sales VALUES
(101,"Lakme Face Souffle",450,'2009-12-26',2659),
(102,"Nike Perfume",400,'2009-01-16',56897),
(103,"Cadbury Chocolate",750,'2009-01-20',65878),
(104,"Dettol Handwash",300,'2009-01-22',45684),
(105,"Lakme Face Souffle",250,'2009-12-26',1456),
(106,"Camay Soap",500,'2009-12-18',7845),
(107,"Red LabelTea",360,'2009-01-02',78961),
(108,"Dettol Handwash",450,'2009-12-11',42217),
(109,"Surf Excel",400,'2009-12-12',24987),
(110,"Pril Dishwasher",450,'2009-01-06',28787);
Table Name : BILL
Customer no. ,Name, bill no.,product name,units sold,
price,advance received,date sold,salesman Id.

CREATE TABLE Bill


(C_no INT PRIMARY KEY NOT NULL,
C_name VARCHAR(30),
Bill_no INT(5) UNIQUE,
Prodname VARCHAR(30),
Unit_sold INT(5),
price DECIMAL(6,2),
adv_recd INT(10),
sales_DATE DATE,
Slm_id INT UNIQUE NOT NULL);
Records: BILL
INSERT INTO bill VALUES
(8071,"Steve Martin",25468,"Nike Perfume",450,425,75000,'2009-11-13',101),
(8072,"Ed Stasium",5789,"Lakme Face Souffle",150,125,10000,'2010-01-13',102),
(8073,"Sam Neill",25445,"Dettol Handwash",400,50,10000,'2009-12-28',103),
(8074,"George Martin",7832,"Bikaji Sweets",200,65,7000,'2009-12-30',104),
(8075,"Eddie Kramer",1579,"Nike Perfume",400,425,100000,'2009-11-13',105),
(8076,"Jeff Jarratt",18798,"Garnier Conditioner",250,100,15000,'2010-01-
23',106),
(8077,"Kevin Costner",21573,"Lakme Moisturiser",100,60,2000,'2010-01-
09',107),
(8078,"Jack Schouzo",5779,"Bikaji Sweets",450,65,13000,'2010-01-15',108),
(8079,"John Cusack",2689,"Lakme Face Souffle",450,425,75000,'2010-01-
06',109),
(8080,"Nathel Parker",98797,"Nike Perfume",300,425,60000,'2009-12-19',110);
Table name :REP Details
Salesman Id, first name, last name, date of appt, commission rate,
weekly sales amount., Monthly target sales
mobile no.,address
(Add 10 records to each of the above tables Using INSERT INTO
command.)

CREATE TABLE REP_Details


(Slm_id INT PRIMARY KEY NOT NULL,
fname CHAR(15),
lname CHAR(15),
Appt_DATE DATE,
Comm DECIMAL(5,2),
Weekly_sales_amt DECIMAL(7,2),
Target_sales INT(5),
Mobile CHAR(15),
addr VARCHAR(30));
Records: REP_DETAILS
INSERT INTO rep_details VALUES
(101,"Harshal","Jogi",'2006-12-15',350.50,12000,8000,9862245891,"12/24,Carol
Bagh"),
(102,"Jeetu","Lamba",'2000-10-06',120,9000,7500,9900235656,"3/25 Shanti
Nagar"),
(103,"Nitesh","More",'1998-05-13',null,600,9000,9856214589,"104/B Nilamber"),
(104,"Kunal","Bhagat",'2003-04-07',325,15000,12000,9865125469,"26 Milap"),
(105,"Lucky","Singh",'2007-03-07',950.75,10250,9000,9356891235,"Pratap
Nagar"),
(106,"Shyam","Sehgal",'2006-09-28',250.25,15000,11000,9874562301,"Fatima
Path"),
(107,"Viraj","Dubey",'2002-11-16',null,10000,12000,9847562301,"Shastri
Nagar"),
(108,"Piyush","Hardikar",'2000-03-22',200,12000,10000,9985620145,"16/C
Cosmos"),
(109,"Sharad","Mehta",'1996-12-27',160,18000,16500,9985624458,"13/B Harsh
Valley"),
(110,"Boman","Boxwala",'2005-06-15',null,6000,7500,9586214722,"Lajpath
Nagar");
Write MySQL queries to perform the following:
Table name : Employee details(EMP_DETAILS)
columns in the table: Employee no.(Primary key),first
name, surname, last name, age, date of birth,
gender,name of the department, designation and
salary.

USE employee;
SELECT * FROM emp_details;
(i)To display Employee no. , first name, surname, last
name of every record.

SELECT emp_no,fname, surname, lname FROM


emp_details;

(ii) To display Employee no. , first name, surname, last


name and age of those age is less than 40 years.

SELECT emp_no,fname, surname, lname,age FROM


emp_details WHERE age<40;
(iii) To display Employee no. , first name, surname, last name and
salary. Use the heading as Emp. No., Name and Salary. Use
concat to display name as one entity combining first name,
surname, last name.

SELECT emp_no as Emp_No,concat(fname," ",surname," ",


lname) as Name, sal as Salary FROM emp_details;

(iv) To display Employee no. , first name, surname, last name and
salary where salary is between 5000 and 10000.

SELECT emp_no,fname, surname, lname,sal FROM emp_details


WHERE sal between 5000 and 10000;
(v) To display Employee no. , first name, surname, last name
whose first name begins with the letter S.

SELECT emp_no,fname, surname, lname,sal FROM emp_details


WHERE fname like "S%";

(vi) To display Employee no. , first name, surname, last name


and age in the order of Surname.

SELECT emp_no,fname, surname, lname, age FROM


emp_details ORDER BY lname ;
(vii) To display Employee no. , first name, surname, last name and
date of birth in chronological order of date of birth.

SELECT emp_no,fname, surname, lname, dob FROM emp_details


ORDER BY dob;

(viii) To display Employee no. , first name, surname, last name and
date of birth of those who are born in the month of June in the
chronological order of date of birth.

SELECT emp_no,fname,surname,lname,dob FROM emp_details


ORDER BY dob WHERE monthname(dob)=”June”;
(ix) To display Employee no. , first name, surname, last name
and salary of highest paid 3 employees.

SELECT emp_no,fname, surname, lname,sal FROM emp_details


ORDER BY sal desc LIMIT 3;

(x) To display name of the dept and total salary grouped


according to dept.

SELECT dept,sum(sal) FROM emp_details GROUP BY dept;


(xi) To display Employee no. , first name, surname and age of all
those who are not in “Sales” dept.

SELECT emp_no,fname, surname,age FROM emp_details WHERE


dept not like "Sales";

(xii)To display Employee no. , first name, surname,dept of


employee in either ‘sales ‘ dept or ‘Marketing’ dept.

SELECT emp_no,fname, surname,dept FROM emp_details WHERE


dept = "Sales" or dept = "Marketing";
(xiii)To display Employee no. , first name, surname,last name of 5
youngest employees.

SELECT emp_no, fname, surname, lname FROM emp_details ORDER


BY age LIMIT 5;

(xiv) To display the next 5 entries starting with the 10th row.

SELECT * FROM emp_details LIMIT 9,5;


(xv)To display names of different departments in the
alphabetical order.

SELECT DISTINCT dept FROM emp_details ORDER BY dept;

(xvi)To display Employee no. , first name, surname and age of


oldest employee.

SELECT emp_no, fname, surname,age FROM emp_details


GROUP BY age desc LIMIT 1;
(xvii) To display Employee no. , first name, surname and age of
youngest employee.

SELECT emp_no, fname, surname,age FROM emp_details GROUP


BY age LIMIT 1;

(xviii) To display name of the dept and average salary for each
dept.

SELECT dept,avg(sal) FROM emp_details GROUP BY dept;


(xix) To display no of employees whose salary is less than 10000.

SELECT count(*) FROM emp_details WHERE sal < 10000;

(xx) To display name of the dept and average age where average age
is more than 30.

SELECT dept, avg(age) FROM emp_details WHERE (select avg(age)


FROM emp_details)>30 GROUP BY dept;
(xxi) To display Employee no. , first name, surname and date of
birth of those who were born between 1970 and 1980.

SELECT emp_no, fname, surname, dob FROM emp_details


WHERE dob BETWEEN 1970 AND 1980;

(xxii) To add one new column to this table to store BONUS.

ALTER TABLE emp_details ADD Bonus DECIMAL(5,2);


(xxiii)To Update the column BONUS @ 50% of salary whenever salary
is less than 10000.

UPDATE emp_details SET bonus=sal *.5 WHERE sal <10000;

(xxiv) To delete the records of all the employees whose age is more
than 55 years.

DELETE FROM emp_details WHERE age>55;

(xxv) To increase salary of “Manager”’s by Rs. 1000.

UPDATE emp_details SET sal=sal+1000 WHERE desig= "Manager";


(i) Table name : Employee details(EMP_DETAILS), employee personal
detail (EMP_PERSONAL)
Write MySQL Statement to display Employee no., first name, surname,
name of the department,designation and salary ,residential address,
phone number, mobile number, email address, appointment date.

SELECT emp_no, fname, surname, dept, desig, sal, addr, ph_no, mobile,
email, app_DATE FROM emp_details INNER JOIN emp_personal USING
(emp_no);
(ii) Table name :FYBCOM _STUDENTS,LIBRARY
Write MySQL Statement to display Admission number , first
name, surname, Book name, accession no., date of issue,
authors name.

USE student;
SELECT adm_no, fname, surname, Bookname, acc_no, dtofissue,
author FROM fybcom_students INNER JOIN library
USING(adm_no);
Table name : Employee details(EMP_DETAILS)
Write MySQL sub query for the following
(i) Display Employee no., first name, surname, department,
designation and salary for those employees who have salary more
than minimum salary for all the records.

USE employee;
SELECT emp_no,fname,surname,dept,desig,sal FROM emp_details
WHERE (select min(sal) FROM emp_details)<sal;
(ii) Display Employee no., first name, surname, department,
designation and salary for those employees who have salary
equal to average salary for all the records.

SELECT emp_no,fname,surname,dept,desig,sal FROM


emp_details WHERE (select avg(sal) FROM emp_details)=sal;

You might also like