Document From Nishad

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 46

IBRAHIM NISHAD AS LAB ON MYSQL

03SU22CC158

INDEX
SL NAME OF PAGE
N EXERCISE NO
O
PART A
1 Create a table EMPLOYEE by specifying the not 4-7
null constraints.
2 Create a table CLIENT_MASTER and write the 8-12
following SQL Queries.
3 Create a table PATIENT and write the following SQL 13-15
Queries.
4 Create a table BOOK and write the following SQL 16-18
Queries.
5 Create two tables SUPPLIER and ITEMS by identifying 19-22
primary and Foreign keys. Specify the not null
property for mandatory keys.
PART B
1 Create the tables EMP_MASTER and ATTENDANCE 24-27
by
identifying Primary and Foreign keys, specify the not
null property for mandatory keys.
2 Create the tables PRODUCTDETAILS and 28-31
PURCHASEDETAILS by identifying primary and
foreign
keys.
3 Create the following tables 32-35
DEPARTMENT_DETAILS and
EMPLOYEE_DETAILS by identifying primary and
foreign keys, specify the not null property for
mandatory keys.
IBRAHIM NISHAD AS LAB ON MYSQL
03SU22CC158
Consider the STUDENT, COURSE and REPORT tables
4 and identify primary and foreign keys, specify the not 36-40
null property for mandatory keys.

Create the SALESORDER, SALES_ORDER_DETAILS


and PRODUCT MASTER tables by identifying primary
5 and foreign keys, specify the not null property for 41-46
mandatory keys.
PART A

3
LOKNATH A LAB ON MYSQL
03SU22CC164
Q1). Create a table Employee by specifying the not null constraints.
Column name Data type Constraints
EmpNo Int Primary key, auto
increment
Name Varchar(25)
Designation Varchar(20)
Dept Varchar(25)
Gender Char(1) ‘M’ or ‘F’
Salary Decimal (9,2)

To create Employee table:


create table employee
-> (Emp_id int primary key auto_increment,
-> Name varchar(25),
-> Designation varchar(25),
-> Dept varchar(25),
-> Gender char(1),
-> Salary decimal(9.2),
-> check ( gender in ('M','F')));
Query OK, 0 rows affected (5.29 sec)

To insert records:
mysql> insert into employee ( Name, Designation, Dept, Gender, Salary) values (‘Nishad’,
'Head', 'Finance', 'M', 120000);
Query OK, 1 row affected (0.78 sec)

Inserted records:
mysql> select * from employee;
+ + + + + + +
| Emp_id | Name | Designation | Dept | Gender | Salary |
+ + + + + + +
| 1 | Nishad | Head | Finance | M | 120000 |
| 2 | Shamil | HR | Sales |M | 75000 |
| 3 | Keerthan | Manager | Marketing | M | 50000 |
| 4 | Sooraj | HR | Marketing | M | 75000 |
| 5 | Nitesh | Manager | Sales |M | 50000 |
| 6 | Kavya | Manager | Finance | F | 50000 |
| 7 | Raksha | HR | Finance | F | 75000 |
| 8 | Karthik | Staff | IT |M | 20000 |
| 9 | Sunil | Staff | Sales |M | 14500 |
| 10 | Ananya | Staff | Marketing | F | 13000 |
+ + + + + + +
10 rows in set (0.44 sec)

4
LOKNATH A LAB ON MYSQL
03SU22CC164
Implement the following queries:
a). Display information about all employees.
mysql> select * from employee;
+ + + + + + +
| Emp_id | Name | Designation | Dept | Gender | Salary |
+ + + + + + +
| 1 | Nishad | Head | Finance | M | 120000 |
| 2 | Shamil | HR | Sales |M | 75000 |
| 3 | Keerthan | Manager | Marketing | M | 50000 |
| 4 | Sooraj | HR | Marketing | M | 75000 |
| 5 | Nitesh | Manager | Sales |M | 50000 |
| 6 | Kavya | Manager | Finance | F | 50000 |
| 7 | Raksha | HR | Finance | F | 75000 |
| 8 | Karthik | Staff | IT |M | 20000 |
| 9 | Sunil | Staff | Sales |M | 14500 |
| 10 | Ananya | Staff | Marketing | F | 13000 |
+ + + + + + +
10 rows in set (0.44 sec)

b). Display Empno, Name and Designation of all employees.


mysql> select Emp_id, Name, Designation from employee;
+ + + +
| Emp_id | Name | Designation |
+ + + +
| 1 | Nishad | Head |
| 2 | Shamil | HR |
| 3 | Keerthan | Manager |
| 4 | Sooraj | HR |
| 5 | Nitesh | Manager|
| 6 | Kavya | Manager|
| 7 | Raksha | HR |
| 8 | Karthik | Staff |
| 9 | Sunil | Staff |
| 10 | Ananya | Staff |
+ + + +
10 rows in set (0.00 sec)

5
LOKNATH A LAB ON MYSQL
03SU22CC164
c). Display the details of all female employees.
mysql> select * from employee where gender='F';
+ + + + + + +
| Emp_id | Name | Designation | Dept | Gender | Salary |
+ + + + + + +
| 6 | Kavya | Manager | Finance | F | 50000 |
| 7 | Raksha | HR | Finance | F | 75000 |
| 10 | Ananya | Staff | Marketing | F | 13000 |
+ + + + + + +
3 rows in set (0.13 sec)

d). List the different departments.


mysql> select distinct(dept) from employee;
+ +
| dept |
+ +
| Finance |
| Sales |
| Marketing |
| IT |
+ +
4 rows in set (0.35 sec)

e). List Empno, Name and Designation of all employees whose salary is more
than 15,000.
mysql> select Emp_id, Name, Designation from employee where salary>15000;
+ + + +
| Emp_id | Name | Designation |
+ + + +
| 1 | Nishad | Head |
| 2 | Shamil | HR |
| 3 | Keerthan | Manager|
| 4 | Sooraj | HR |
| 5 | Nitesh | Manager |
| 6 | Kavya | Manager |
| 7 | Raksha | HR |
| 8 | Karthik | Staff |
+ + + +
8 rows in set (0.07 sec)

6
LOKNATH A LAB ON MYSQL
03SU22CC164
f). Display the highest and lowest salary of each department.
mysql> select dept, max(salary), min(salary) from employee group by dept;
+ + + +
| dept | max(salary) | min(salary) |
+ + + +
| Finance | 120000 | 50000 |
| Sales | 75000 | 14500 |
| Marketing | 75000 | 013000 |
| IT | 20000 | 20000 |
+ + + +
4 rows in set (0.16 sec)

g). Display the number of employees in each department.


mysql> select dept, count(*) from employee group by dept;
+ + +
| dept | count(*) |
+ + +
| Finance | 3 |
| Sales | 3 |
| Marketing | 3 |
| IT | 1 |
+ + +
4 rows in set (0.04
sec)

h). Display the number of employees in Marketing and Sales department.


mysql> select dept, count(*) from employee where dept in ('Marketing', 'Sales') group by
dept;
+ + +
| dept | count(*) |
+ + +
| Sales | 3 |
| Marketing | 3|
+ + +
2 rows in set (0.00
sec)

7
LOKNATH A LAB ON MYSQL
03SU22CC164
Q2). Create a table Client_master with the following fields.
Column_name Data Type Others
Client_no Varchar(6) Primary key
Name Varchar(20) Not null
Address Varchar(25)
City Varchar(20)
Pincode Decimal(6)
State Varchar(20)
Bal_due Decimal(7,2) Not null

To create Client_master table:


mysql> create table client_master
-> (Client_no varchar(6) Primary key,
-> Name varchar(20) not null,
-> Address varchar(25),
-> City varchar(20),
-> Pincode Decimal(6),
-> State Varchar(20),
-> Bal_due Decimal(7.2) Not null);
Query OK, 0 rows affected (0.34 sec)

To insert records:
mysql> insert into client_master values('C01001', ‘Loknath’, 'Padil', 'Mangalore',
575006,'Karnataka', 500);
Query OK, 1 row affected (0.20 sec)

Inserted records:
+ + + + + + + + +
| Client_no | Name | Address | City | Pincode | State | Bal_due |
+ + + + + + + +
| C01001 | Nishad | Padil | Mangalore | 575006 | Karnataka | 500 |
| C01002 | Shamil | Kottara | Mangalore | 575010 | Karnataka | 700 |
| C01003 | Keerthan | Bejai | Mangalore | 575007 | Karnataka | 300 |
| C01004 | Sooraj | Pandeshwar | Mangalore | 575001 | Karantaka | 450 |
| C01005 | Nitesh | Kunjathbail | Mangalore | 575003 | Karantaka | 500 |
| C01006 | Shiva | Kadri | Mangalore | 575005 | Karantaka | 550 |
| C01007 | Roshan | Thalapady | Mangalore | 575009 | Karantaka | 300 |
| C01008 | Karthik | Jyothi | Mangalore | 575002 | Karantaka | 400 |
| C01009 | Jishnu | Kadri | Mangalore | 575005 | Karantaka | 650 |
| C01010 | Gourav | Pandeshwar | Mangalore | 575001 | Karantaka | 700 |
+ + + + + + + +
10 rows in set (0.18 sec)

8
LOKNATH A LAB ON MYSQL
03SU22CC164
Implement the following queries:
a). Describe the structure of Client master.
mysql> describe client_master;
+ + + -+ + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| Client_no | varchar(6) | NO | PRI | NULL | |
| Name | varchar(20) | NO | | NULL | |
| Address | varchar(25) | YES | | NULL | |
| City | varchar(20) | YES | | NULL | |
| Pincode | decimal(6,0) | YES | | NULL | |
| State | varchar(20) | YES | | NULL | |
| Bal_due | decimal(7,2) | NO | | NULL | |
+ + + + + + +
7 rows in set (13.52 sec)

b). From the table Client_master create a new table Client1 that contains
only Client_no, Name with all records of Client_master.
mysql> create table client1 as (select client_no, name from client_master);
Query OK, 10 rows affected (7.42 sec)
Records: 10 Duplicates: 0 Warnings: 0

mysql> select * from client1;


+ + +
| client_no | name |
+ + +
| C01001 | Nishad |
| C01002 | Shamil |
| C01003 | Keerthan |
| C01004 | Sooraj |
| C01005 | Nitesh |
| C01006 | Shiva |
| C01007 | Roshan |
| C01008 | Karthik |
| C01009 | Jishnu |
| C01010 | Gourav |
+ + +
10 rows in set (0.00
sec)

9
LOKNATH A LAB ON MYSQL
03SU22CC164
c). From the table Client_master create a new table Client2 that has the same
structure as Client_Master but with no records.
mysql> create table client2 as ( select * from client_master where 1=2);
Query OK, 0 rows affected (4.81 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> select * from client2;


Empty set (0.00 sec)

mysql> desc client2;


+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| Client_no | varchar(6) | NO | | NULL | |
| Name | varchar(20) | NO | | NULL | |
| Address | varchar(25) | YES | | NULL | |
| City | varchar(20) | YES | | NULL | |
| Pincode | decimal(6,0) | YES | | NULL | |
| State | varchar(20) | YES | | NULL | |
| Bal_due | decimal(7,2) | NO | | NULL | |
+ + + + + + +
7 rows in set (2.77 sec)

d). Insert records into table Client3 from the Client_master table where the Client_no
is ‘C01001’.
mysql> create table client3 as (select * from client_master where client_no='C01001');
Query OK, 1 row affected (2.29 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> select * from client3;


+ + + + + + + +
| Client_no | Name | Address | City | Pincode | State | Bal_due |
+ + + + + + + +
| C01001 | Nishad | Padil | Mangalore | 575006 | Karnataka | 500 |
+ + + + + + + +
1 row in set (0.02 sec)

10
LOKNATH A LAB ON MYSQL
03SU22CC164
e). For every client in Client_master table increase the Bal_due by 10%.
mysql> update client_master set Bal_due=bal_due+((10/100)*bal_due);
Query OK, 10 rows affected (0.20 sec)
Rows matched: 10 Changed: 10 Warnings: 0

mysql> select * from client_master;


+ + + + + + + +
| Client_no | Name | Address | City | Pincode | State | Bal_due |
+ + + + + + + +
| C01001 | Nishad | Padil | Mangalore | 575006 | Karnataka | 550 |
| C01002 | Shamil | Kottara | Mangalore | 575010 | Karnataka | 770 |
| C01003 | Keerthan | Bejai | Mangalore | 575007 | Karnataka | 330 |
| C01004 | Sooraj | Pandeshwar | Mangalore | 575001 | Karantaka | 495 |
| C01005 | Nitesh | Kunjathbail | Mangalore | 575003 | Karantaka | 550 |
| C01006 | Shiva | Kadri | Mangalore | 575005 | Karantaka | 715 |
| C01007 | Roshan | Thalapady | Mangalore | 575009 | Karantaka | 330 |
| C01008 | Karthik | Jyothi | Mangalore | 575002 | Karantaka | 440 |
| C01009 | Jishnu | Kadri | Mangalore | 575005 | Karantaka | 693 |
| C01010 | Gourav | Pandeshwar | Mangalore | 575001 | Karantaka | 770 |
+ + + + + + + +
10 rows in set (0.04 sec)

f).Update table Client_master change the contents of the field Name to ‘VijayKadam’ &
the contents of the field Address to ‘SCT Jay Apmts’ for the record with Client_no
‘C00002’.
mysql> update client_master set name='Vijay Kadam', address='Sct Jay Apmts' where
client_no='C01002';
Query OK, 1 row affected (0.18 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from client_master;
+ + + + + + + +
| Client_no | Name | Address | City | Pincode | State | Bal_due |
+ + + + + + + +
| C01001 | Nishad | Padil | Mangalore | 575006 | Karnataka | 550 |
| C01002 | Vijay Kadam | Sct Jay Apmts | Mangalore | 575010 | Karnataka | 770 |
| C01003 | Keerthan | Bejai | Mangalore | 575007 | Karnataka | 330 |
| C01004 | Sooraj | Pandeshwar | Mangalore | 575001 | Karantaka | 495 |
| C01005 | Nitesh | Kunjathbail | Mangalore | 575003 | Karantaka | 550 |
| C01006 | Shiva | Kadri | Mangalore | 575005 | Karantaka | 605 |
| C01007 | Roshan | Thalapady | Mangalore | 575009 | Karantaka | 330 |
| C01008 | Karthik | Jyothi | Mangalore | 575002 | Karantaka | 440 |
| C01009 | Jishnu | Kadri | Mangalore | 575005 | Karantaka | 693 |
| C01010 | Gourav | Pandeshwar | Mangalore | 575001 | Karantaka | 770 |
+ + + + + + + +
10 rows in set (0.00 sec)

11
LOKNATH A LAB ON MYSQL
03SU22CC164
g). Add a new column by name Penalty number (10,2) to table Client_master.
mysql> alter table client_master add(Penalty decimal(10,2));
Query OK, 0 rows affected (1.14 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc client_master;


+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| Client_no | varchar(6) | NO | PRI | NULL | |
| Name | varchar(20) | NO | | NULL | |
| Address | varchar(25) | YES | | NULL | |
| City | varchar(20) | YES | | NULL | |
| Pincode | decimal(6,0) | YES | | NULL | |
| State | varchar(20) | YES | | NULL | |
| Bal_due | decimal(7,2) | NO | | NULL | |
| Penalty | decimal(10,2) | YES | | NULL | |
+ + + + + + +
8 rows in set (0.06 sec)

h). Change the size of the column Penalty to (8,2) in Client_master.


mysql> alter table client_master modify Penalty decimal(8,2);
Query OK, 10 rows affected (2.38 sec)
Records: 10 Duplicates: 0 Warnings: 0

mysql> desc client_master;


+ + + + + + +
| Field | Type | Null | Key | Default | Extra |
+ + + + + + +
| Client_no | varchar(6) | NO | PRI | NULL | |
| Name | varchar(20) | NO | | NULL | |
| Address | varchar(25) | YES | | NULL | |
| City | varchar(20) | YES | | NULL | |
| Pincode | decimal(6,0) | YES | | NULL | |
| State | varchar(20) | YES | | NULL | |
| Bal_due | decimal(7,2) | NO | | NULL | |
| Penalty | decimal(8,2) | YES | | NULL | |
+ + + + + + +
8 rows in set (0.02 sec)

i). Change the name of table Client_master to Client_master1.


mysql> rename table client_master to client_master1;
Query OK, 0 rows affected (0.29 sec)

12
LOKNATH A LAB ON MYSQL
03SU22CC164
Q3). Create a table Patient.

Column name Data type Null Others


PatientID Int Not null Primary key,
auto increment.
Name Varchar(25) Not null
DateofBirth Date Not null
Lastvisitdate Date Not null
Nextvisitdate Date Null

To create Patient table:


mysql> create table patient
-> (PatientID int primary key auto_increment,
-> Name varchar(25) not null,
-> DOB Date not null,
-> LVdate Date not null,
-> NVdate Date null);
Query OK, 0 rows affected (0.25 sec)

To insert records:
mysql> insert into patient( Name, DOB, LVdate, NVdate) values( Nishad, '2005-
04- 04','2023-05-20', '2023-06-20');
Query OK, 1 row affected (0.20 sec)

Inserted records:
mysql> select * from patient;
+ + + + + +
| PatientID | Name | DOB | LVdate | NVdate |
+ + + + + +
| 1 | Nishad | 2005-04-04 | 2023-05-20 | 2023-06-20 |
| 2 | Shyam | 2000-11-15 | 2023-01-25 | 2023-12-25 |
| 3 | Thanoof | 2003-12-05 | 2023-04-11 | 2023-06-20 |
| 4 | Shamil | 2001-03-10 | 2023-05-11 | 2023-06-20 |
| 5 | Shuhail | 2005-10-09 | 2023-06-27 | 2023-07-30 |
| 6 | Nitesh | 2004-01-10 | 2023-03-28 | 2023-04-20 |
| 7 | Shiva | 2000-05-28 | 2023-04-16 | 2023-06-16 |
| 8 | Yakshith | 2002-06-15 | 2023-05-01 | 2023-06-30 |
| 9 | Gourav | 2003-10-05 | 2023-01-09 | 2023-03-26 |
| 10 | Jaganath | 2002-07-12 | 2023-05-11 | 2023-06-10 |
+ + + + + +
10 rows in set (0.00 sec)

13
LOKNATH A LAB ON MYSQL
03SU22CC164
Implement the following queries:
a). Display patients who were born in a particular year and sort by birth-month.
mysql> select * from patient where year(DOB)= 2000 order by (DOB);
+ + + + + +
| PatientID | Name | DOB | LVdate | NVdate |
+ + + + + +
| 7 | Shiva | 2000-05-28 | 2023-04-16 | 2023-06-16 |
| 2 | Shyam | 2000-11-15 | 2023-01-25 | 2023-02-28 |
+ + + + + +
2 rows in set (0.04 sec)

b). List the age of all the patients.


mysql> select round(datediff(curdate(),DOB)/365) 'age' from patient;
+ +
| age |
+------+
| 18 |
| 23 |
| 20 |
| 22 |
| 18 |
| 19 |
| 23 |
| 21 |
| 20 |
| 21 |
+------+
10 rows in set (0.07 sec)

c). Display the names of patients who are 18 years old or younger.
mysql> select name from patient where( round(datediff(curdate(),DOB)/365)<=18);
+ +
| name |
+ +
| Nishad |
| Shuhail |
+ +
2 rows in set (0.00 sec)

14
LOKNATH A LAB ON MYSQL
03SU22CC164
d). Schedule the next visit of ‘Shyam’ to be 6 months from now.
mysql> update patient set NVdate =date_add(now(), interval 6 month) where name='Shyam';
Query OK, 1 row affected, 1 warning (0.11 sec)
Rows matched: 1 Changed: 1 Warnings: 1

mysql> select * from patient;


+ + + + + +
| PatientID | Name | DOB | LVdate | NVdate |
+ + + + + +
| 1 | Nishad | 2005-04-04 | 2023-05-20 | 2023-06-20 |
| 2 | Shyam | 2000-11-15 | 2023-01-25 | 2023-12-25 |
| 3 | Thanoof | 2003-12-05 | 2023-04-11 | 2023-06-20 |
| 4 | Shamil | 2001-03-10 | 2023-05-11 | 2023-06-20 |
| 5 | Sooraj | 2005-10-09 | 2023-06-27 | 2023-07-30 |
| 6 | Nitesh | 2004-01-10 | 2023-03-28 | 2023-04-20 |
| 7 | Shiva | 2000-05-28 | 2023-04-16 | 2023-06-16 |
| 8 | Yakshith | 2002-06-15 | 2023-05-01 | 2023-06-30 |
| 9 | Gourav | 2003-10-05 | 2023-01-09 | 2023-03-26 |
| 10 | Jaganath | 2002-07-12 | 2023-05-11 | 2023-06-10 |
+ + + + + +
10 rows in set (0.00 sec)

15
LOKNATH A LAB ON MYSQL
03SU22CC164
Q4). Create a table book.
Column_Name Data_Type Others
Book_Id Varchar(5) Primary Key
Title Varchar(25) Not Null
Publisher Varchar(25) Not Null
Category Varchar(20)
Year Year
Price Decimal(6,2)

To create Book table:


mysql> create table book
-> (BookID varchar(5) Primary key,
-> Title varchar(25) not null,
-> Publisher varchar(25) not null,
-> Category varchar(20),
-> Year year,
-> Price Decimal(6,2));
Query OK, 0 rows affected (0.53 sec)

To insert records:
mysql> insert into book values('B001', ' Basics of Networking', 'Microsoft press', 'Computer',
'2005', 1570);
Query OK, 1 row affected (0.12 sec)

Inserted records:
mysql> select * from book;
+ + + + + + +
| BookID | Title | Publisher | Category | Year | Price |
+ + + + + + +
| B001 | Basics of Networking | Microsoft press | Computer | 2005 | 1570.00 |
| B002 | C Basics | Manning publications | Computer | 2010 | 2050.00 |
| B003 | Basics of Networking | Devops | Computer | 2009 | 740.00 |
| B004 | Linux Basics | Occupy the Web | Computer | 2015 | 2500.00 |
| B005 | Web Hacking | David Bombal | Computer | 2010 | 1700.00 |
| B006 | Network Hacking | David Bombal | Computer | 2015 | 2750.00 |
| B007 | Java for beginners | Microsoft press | Computer | 2012 | 1550.00 |
| B008 | Andriod Hacking | Gary Micconin | Computer | 2017 | 2900.00 |
| B009 | Internet Basics | Microsoft Press | Computer | 2007 | 1200.00 |
| B010 | Penetration Testing | Manning publications | Computer | 2010 | 1800.00 |
+ + + + + + +
10 rows in set (0.21 sec)

16
LOKNATH A LAB ON MYSQL
03SU22CC164
Implement the following queries:
a). List the details of the publishers starting with the character ‘M’.
mysql> select * from book where Publisher like 'M%';
+ + + + + + +
| BookID | Title | Publisher | Category | Year | Price |
+ + + + + + +
| B001 | Basics of Networking | Microsoft press | Computer | 2005 | 1570.00 |
| B002 | C Basics | Manning publications | Computer | 2010 | 2050.00 |
| B007 | Java for beginners | Microsoft press | Computer | 2012 | 1550.00 |
| B009 | Internet Basics | Microsoft Press | Computer | 2007 | 1200.00 |
| B010 | Penetration Testing | Manning publications | Computer | 2010 | 1800.00 |
+ + + + + + +
5 rows in set (0.14 sec)

b). List the publishers having ‘a’ as the second character in their names.
mysql> select publisher from book where Publisher like '_a%';
+ +
| publisher |
+ +
| Manning publications |
| David Bombal |
| David Bombal |
| Gary Micconin |
| Manning publications |
+ +
5 rows in set (0.00 sec)

c). Find the books published in the year 2010.


mysql> select * from book where year= 2010;
+ + + + + + +
| BookID | Title | Publisher | Category | Year | Price |
+ + + + + + +
| B002 | C Basics | Manning publications | Computer | 2010 | 2050.00 |
| B005 | Web Hacking | David Bombal | Computer | 2010 | 1700.00 |
| B010 | Penetration Testing | Manning publications | Computer | 2010 | 1800.00 |
+ + + + + + +
3 rows in set (0.00 sec)

17
LOKNATH A LAB ON MYSQL
03SU22CC164
d). Display the Book_ID, Title and Publisher of all books in the descending order of
the year.
mysql> select BookID, Title, Publisher from book order by year desc;
+ + + +
| BookID | Title | Publisher |
+ + + +
| B008 | Andriod Hacking | Gary Micconin |
| B004 | Linux Basics | Occupy the Web |
| B006 | Network Hacking | David Bombal |
| B007 | Java for beginners | Microsoft press |
| B002 | C Basics | Manning publications |
| B005 | Web Hacking | David Bombal |
| B010 | Penetration Testing | Manning publications |
| B003 | Basics of Networking | Devops |
| B009 | Internet Basics | Microsoft Press |
| B001 | Basics of Networking | Microsoft press |
+ + + +
10 rows in set (0.00 sec)

e). Display the details of all books other than Microsoft Press publishers.
mysql> select * from book where publisher not in ('Microsoft press');
+ + + + + + +
| BookID | Title | Publisher | Category | Year | Price |
+ + + + + + +
| B002 | C Basics | Manning publications | Computer | 2010 | 2050.00 |
| B003 | Basics of Networking | Devops | Computer | 2009 | 740.00 |
| B004 | Linux Basics | Occupy the Web | Computer | 2015 | 2500.00 |
| B005 | Web Hacking | David Bombal | Computer | 2010 | 1700.00 |
| B006 | Network Hacking | David Bombal | Computer | 2015 | 2750.00 |
| B008 | Andriod Hacking | Gary Micconin | Computer | 2017 | 2900.00 |
| B010 | Penetration Testing | Manning publications | Computer | 2010 | 1800.00 |
+ + + + + + +
7 rows in set (0.00 sec)

f). Display Title, Price of all books with Price more than 2000 & less than 3000.
(Using ‘Between’ operator).
mysql> select title, price from book where price between 2000 and 3000;
+ + +
| title | price |
+ + +
| C Basics | 2050.00 |
| Linux Basics | 2500.00 |
| Network Hacking | 2750.00 |
| Andriod Hacking | 2900.00 |
+ + +
4 rows in set (0.05 sec)

18
LOKNATH A LAB ON MYSQL
03SU22CC164
Q5). Create the following tables by identifying primary and foreign keys.
Specify the not null property for mandatory keys.
SUPPLIERS (SNO, SNAME, SADDR, CITY).
ITEMS (INO, SNO, INAME, QTY).

To create Suppliers table:

mysql> create table suppliers


-> (Sno varchar(5) primary key,
-> Sname varchar(25) not null,
-> Saddr varchar(15) not null,
-> city varchar(15) not null);
Query OK, 0 rows affected (2.52
sec)

To insert records:
mysql> insert into suppliers values('S001', 'Microtech', 'Pumpwell', 'Mangalore');
Query OK, 1 row affected (0.07 sec)

Inserted records:
mysql> select * from suppliers;
+ + + + +
| Sno | Sname | Saddr | city |
+ + + + +
| S001 | Microtech | Pumpwell | Mangalore |
| S002 | Cats | Jayanagara | Bangalore |
| S003 | Microtech | Kadri | Mangalore |
| S004 | Microtech | Pandeshwar | Mangalore |
| S005 | Electrotech | JPnagar | Bangalore |
| S006 | Cats | MGroad | Mysore |
| S007 | Cats | Church road | Bangalore |
| S008 | Electrotech | Kulshekar | Mangalore |
| S009 | Microtech | Jeppu | Mangalore |
| S010 | Cats | Bejai | Mangalore |
+ + + + +
10 rows in set (0.00 sec)

19
LOKNATH A LAB ON MYSQL
03SU22CC164
To create Items table:
mysql> create table items
-> ( Ino varchar(5) primary key,
-> Sno varchar(5),
-> Iname varchar(25) not null,
-> Qty int(3) not null,
-> foreign key(sno) references suppliers(sno));
Query OK, 0 rows affected, 1 warning (0.50 sec)

To insert records:
mysql> insert into items values('I001', 'S001', 'Keyboard', 5);
Query OK, 1 row affected (0.12 sec)

Inserted records:
mysql> select * from items;
+ + + + +
| Ino | Sno | Iname | Qty |
+ + + + +
| I001 | S001 | Keyboard | 5 |
| I002 | S002 | Monitor | 10 |
| I003 | S003 | Pendrive | 7 |
| I004 | S004 | Pen | 25 |
| I005 | S005 | Keyboard | 15 |
| I006 | S006 | Pendrive | 20 |
| I007 | S007 | Mobile | 4 |
| I008 | S008 | Keyboard | 19 |
| I009 | S009 | Monitor | 12 |
| I010 | S010 | Keyboard | 9 |
+ + + + +
10 rows in set (0.00 sec)

20
LOKNATH A LAB ON MYSQL
03SU22CC164
Implement the following queries:

a). List items and suppliers details.


mysql> select sname, iname from suppliers, items where suppliers.sno= items.sno;
+ + +
| sname | iname |
+ + +
| Microtech | Keyboard |
| Cats | Monitor |
| Microtech | Pendrive |
| Microtech | Pen |
| Electrotech | Keyboard |
| Cats | Pendrive |
| Cats | Mobile |
| Electrotech | Keyboard |
| Microtech | Monitor |
| Cats | Keyboard |
+ + +
10 rows in set (0.01
sec)

b). List the names of the suppliers who are supplying Keyboard.
mysql> select sname from suppliers, items where suppliers.sno = items.sno and
iname='Keyboard';
+ +
| sname |
+ +
| Microtech |
| Electrotech |
| Electrotech |
| Cats |
+ +
4 rows in set (0.00 sec)

c). Display the items supplied by ‘Microtech’.


mysql> select iname from suppliers, items where suppliers.sno= items.sno and
sname='Microtech';
+ +
| iname |
+ +
| Keyboard |
| Pendrive |
| Pen |
| Monitor |
+ +
4 rows in set (0.00 sec)
21
LOKNATH A LAB ON MYSQL
03SU22CC164
d). List the items supplied by the suppliers ‘Cats’ and ‘Electrotech’.
mysql> select iname from suppliers, items where suppliers.sno= items.sno and sname in
('Electrotech','Cats');
+ +
| iname |
+ +
| Monitor |
| Keyboard |
| Pendrive |
| Mobile |
| Keyboard |
| Keyboard |
+ +
6 rows in set (0.00 sec)

22
LOKNATH A LAB ON MYSQL
03SU22CC164

PART B

23
LOKNATH A LAB ON MYSQL
03SU22CC164
Q1). Create two tables Emp_master and Attendance with
the followingfields.
Emp_master (emp_id, ename, eaddr, phone, email, doj, dor),
Attendance (emp_id, wom, mhrs, thrs, whrs, trhrs, fhrs, shrs,
suhrs).
Identify Primary and Foreign keys, specify the not null
property for mandatory keys.
(Check constraint should be applied for wom<=5 and doj).

To create Emp_master table:


mysql> create table emp_master
-> (Emp_id varchar(4) primary key,
-> Ename varchar(20) not null,
-> Eaddr varchar(20) not null,
-> Phone bigint(12),
-> Email varchar(20) not null,
-> DOJ date,
-> DOR date,
-> check (DOJ<DOR));
Query OK, 0 rows affected, 1 warning (1.16 sec)

To insert records:
mysql> insert into emp_master values( 'E001', 'Mahesh', 'Udupi', 9444851168,
'[email protected]', '2015-01-25', '2019-07-15');
Query OK, 1 row affected (0.15 sec)

Inserted records:
mysql> select * from emp_master;
+ + + + + + + +
| Emp_id | Ename | Eaddr | Phone | Email | DOJ | DOR |
+ + + + + + + +
| E001 | Mahesh | Udupi | 9444851168 | [email protected] | 2015-01-25 | 2019-07-15 |
| E002 | Nishad | Mangalore | 9458941168 | [email protected]| 2018-12-07 | 2024-11-22 |
| E003 | Shamil| Bangalore | 6368941168 | [email protected]| 2013-11-08 | 2022-05-20 |
| E004 | Nitesh | Bangalore | 9855941168 | [email protected] | 2015-05-05 | 2023-01-15 |
| E005 | Karthik | Udupi | 9885491168 | [email protected] | 2019-11-16 | 2026-10-01 |
| E006 | Shiva | Kerala | 9546491168 | [email protected] | 2019-07-10 | 2024-09-11 |
| E007 | Nikhith | Kerala | 9546456168 | [email protected] | 2010-05-28 | 2020-05-15 |
| E008 | Sooraj | Mumbai | 9578910168 | [email protected] | 2020-05-11 | 2025-11-13 |
| E009 | Prasad | Mangalore | 9578910078 | [email protected] | 2019-12-18 | 2023-04-10 |
| E010 | Gowtham| Bangalore| 9894610078 | [email protected]| 2020-12-28| 2025-07-19 |
+ + + + + + + +
10 rows in set (0.00 sec)

24
LOKNATH A LAB ON MYSQL
03SU22CC164
To create Attnd table:
mysql> create table attnd
-> (emp_id varchar(4),
-> wom int(1),
-> mhrs int(2),
-> thrs int(2),
-> whrs int(2),
-> trhrs int(2),
-> fhrs int(2),
-> shrs int(2),
-> suhrs int(2),
-> check(wom<=5),
-> foreign key(emp_id) references Emp_master(emp_id) on delete cascade);
Query OK, 0 rows affected, 8 warnings (1.18 sec)

To insert records:
mysql> insert into attnd values('E001',0,0,0,0,0,0,0,0);
Query OK, 1 row affected (0.21 sec)

Inserted records:
mysql> select * from attnd;
+ + + + + + + + + +
| emp_id | wom | mhrs | thrs | whrs | trhrs | fhrs | shrs | suhrs |
+ + + + + + + + + +
| E001 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0|
| E002 | 1 | 1 | 0 | 1 | 0 | 0 | 2 | 2 |
| E003 | 1 | 1 | 4 | 3 | 2 | 4 | 2 | 2 |
| E004 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| E005 | 4 | 0 | 3 | 5 | 4 | 2 | 1 | 3 |
| E006 | 0 | 0 | 3 | 1 | 3 | 2 | 1 | 3 |
| E007 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| E008 | 1 | 5 | 5 | 4 | 3 | 2 | 5 | 1 |
| E009 | 1 | 5 | 3 | 4 | 3 | 6 | 5 | 3 |
| E010 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+ + + + + + + + + +
10 rows in set (0.00 sec)

25
LOKNATH A LAB ON MYSQL
03SU22CC164
Implement the following queries:
a). Display Ename and Email of all employees who are working on Sunday.
mysql> select ename, email from emp_master where emp_id in( select emp_id from attnd
where suhrs>0);
+ + +
| ename | email |
+ + +
| Nishad | [email protected] |
| Shamil | [email protected] |
| Karthik | [email protected] |
| Shiva | [email protected] |
| Sooraj | [email protected] |
| Prasad | [email protected] |
+ + +
6 rows in set (0.20 sec)

b). Display the total hours worked by employee Mahesh.


mysql> select mhrs+thrs+whrs+trhrs+fhrs+shrs+suhrs "Mahesh hours" from attnd where
emp_id=(select emp_id from emp_master where ename='Mahesh');
+ +
| Mahesh hours |
+ +
| 0|
+ +
1 row in set (0.07 sec)

c). Display the names of the employees who never attended the duty so far.
mysql> select ename from emp_master where emp_id in( select emp_id from attnd where
mhrs+thrs+whrs+trhrs+fhrs+shrs+suhrs=0);
+ +
| ename |
+ +
| Mahesh |
| Nitesh |
| Nikhith |
| Gowtham |
+ +
4 rows in set (0.00 sec)

26
LOKNATH A LAB ON MYSQL
03SU22CC164
d). Delete the records of the employees who are already retired.
mysql> delete from emp_master where dor<curdate();
Query OK, 5 rows affected (0.21 sec)

mysql> select * from emp_master;


+ + + + + + + +
| Emp_id | Ename | Eaddr | Phone | Email | DOJ | DOR |
+ + + + + + + +
| E002 | Nishad | Mangalore| 9458941168 | [email protected] |2018-12-07 | 2024-11-22 |
| E005 | Karthik | Udupi | 9885491168 | [email protected] | 2019-11-16 | 2026-10-01 |
| E006 | Shiva | Kerala | 9546491168 | [email protected] | 2019-07-10 | 2024-09-11 |
| E008 | Sooraj | Mumbai | 9578910168 | [email protected] | 2020-05-11 | 2025-11-13 |
| E010 | Gowtham| Bangalore | 9894610078 | [email protected]| 2020-12-28| 2025-07-19|
+ + + + + + + +
5 rows in set (0.00 sec)

e). Display the names of the employees who have total no. of hours more than 20hrs
a week.
mysql> select ename from emp_master where emp_id in( select emp_id from attnd where
mhrs+thrs+whrs+trhrs+fhrs+shrs+suhrs>20);
+ +
| ename |
+ +
| Sooraj |
+ +
1 row in set (0.00 sec)

27
LOKNATH A LAB ON MYSQL
03SU22CC164
Q2). Create the following tables by identifying primary and foreign keys,
specify the not null property for mandatory keys.

PRODUCT_DETAIL
PRODNO PNAME QTY_AVAIL PRICE PROFIT
In%
P0001 Monitor 10 1500 25
P0002 Pen Drive 50 250 5
P0003 CD Drive 5 950 8
P0004 Keyboard 8 250 10

PURCHASED_DETAIL
CNAME PRODNO QTY_SOLD
Raman P0003 2
Laxman P0002 5
Bharath P0002 10
Manish P0001 3
Amith P0004 2

To create Product detail table:


mysql> create table Prod_detail
-> (Prodno varchar(6) primary key,
-> Pname varchar(15) not null,
-> Qty_avail int (3) not null,
-> Price int(5) not null,
-> Profit int(3));
Query OK, 0 rows affected, 3 warnings (0.28 sec)

To insert records:
mysql> insert into prod_detail values('P0001', 'Monitor', 10, 1500, 25);
Query OK, 1 row affected (0.09 sec)

Inserted records:
mysql> select * from Prod_detail;
+ + + + + +
| Prodno | Pname | Qty_avail | Price | Profit |
+ + + + + +
| P0001 | Monitor | 10 | 1500 | 25 |
| P0002 | Pen Drive | 50 | 250 | 5|
| P0003 | CD Drive | 5 | 950 | 8|
| P0004 | Keyboard | 8 | 250 | 10 |
+ + + + + +
4 rows in set (0.00 sec)

28
LOKNATH A LAB ON MYSQL
03SU22CC164
To create Purchased detail table:
mysql> create table Pur_detail
-> (Cname varchar(10) not null,
-> Prodno varchar(6),
-> Qty_sold int(4),
-> foreign key(prodno) references prod_detail(prodno));
Query OK, 0 rows affected, 1 warning (1.09 sec)

To insert records:
mysql> insert into pur_detail values( 'Raman', 'P0003', 2);
Query OK, 1 row affected (0.10 sec)

Inserted records:
mysql> select * from Pur_detail;
+ + + +
| Cname | Prodno | Qty_sold |
+ + + +
| Raman | P0003 | 2|
| Laxman | P0002 | 5|
| Bharath | P0002 | 10 |
| Manish | P0001 | 3|
| Amith | P0004 | 2|
+ + + +
5 rows in set (0.00 sec)

29
LOKNATH A LAB ON MYSQL
03SU22CC164
Implement the following queries:

a). Display the total amount spent by Mr. Laxman for purchasing any product.
mysql> select (price * QTY_sold) "Laxman Expenses" from prod_detail, pur_detail
where Prod_detail.prodno=pur_detail.prodno and cname='Laxman';
+ +
| Laxman Expenses |
+ +
| 1250 |
+ +
1 row in set (0.05 sec)

b). Display the names of product for which either quantity available is less than 10
or quantity sold is less than 4.
mysql> select pname from prod_detail,pur_detail where
prod_detail.prodno=pur_detail.prodno and (Qty_avail<10 or Qty_sold<4);
+ +
| pname |
+ +
| CD Drive |
| Monitor |
| Keyboard |
+ +
3 rows in set (0.87 sec)

c). Display the names of product and quantity taken by Mr. Bharath.
mysql> select pname,Qty_sold from prod_detail,pur_detail where
prod_detail.prodno=pur_detail.prodno and cname='Bharath';
+ + +
| pname | Qty_sold |
+ + +
| Pen Drive | 10 |
+ + +
1 row in set (0.05
sec)

d). What is the Profit earned by the shopkeeper on Manish’s purchase?.


mysql> select price * qty_sold*profit/100 "Profit on Manish" from prod_detail,pur_detail
where prod_detail.prodno=pur_detail.prodno and cname='Manish';
+ +
| Profit on Manish |
+ +
| 1125.0000 |
+ +
1 row in set (0.03 sec)

30
LOKNATH A LAB ON MYSQL
03SU22CC164
e). How many pen drives were sold?.
mysql> select sum(qty_sold) "Pen drive sold" from prod_detail, pur_detail where
prod_detail.prodno=pur_detail.prodno and pname='Pen drive';
+ +
| Pendrive sold |
+ +
| 15 |
+ +
1 row in set (0.28 sec)

31
LOKNATH A LAB ON MYSQL
03SU22CC164
Q3). Create the following tables by identifying primary and foreign keys,
specify the not null property for mandatory keys.

DEPARTMENT_DETAILS
Deptno Deptname Totemp Charge/hr Bonus
D0001 Computer 5 250 2
D0002 Maths 6 300 4
D0003 Chemistry 5 200 3
D0004 Physics 4 225 1

EMPLOYEE_DETAILS
Empname Deptno Total hours
Ramu D0002 8
Bimu D0001 6
Ramanath D0003 4
Somu D0002 7
Diren D0004 5

To create Department details table:


mysql> create table dept_det
-> (deptno varchar(5) primary key,
-> dname varchar(20) not null,
-> totemp int(2),
-> charge int(4),
-> bonus int(2));
Query OK, 0 rows affected, 3 warnings (7.36 sec)

To insert records:
mysql> insert into dept_det values('D0001', 'Computer', 5, 250, 2);
Query OK, 1 row affected (0.42 sec)

Inserted records:
mysql> select * from dept_det;
+ + + + + +
| deptno | dname | totemp | charge | bonus |
+ + + + + +
| D0001 | Computer | 5 | 250 | 2 |
| D0002 | Maths | 6 | 300 | 4 |
| D0003 | Chemistry | 5 | 200 | 3 |
| D0004 | Physics | 4 | 225 | 1 |
+ + + + + +
4 rows in set (0.00 sec)

32
LOKNATH A LAB ON MYSQL
03SU22CC164
To create Employee details table:
mysql> create table emp_det
-> (ename varchar(20) not null,
-> deptno varchar(5),
-> hours int(2),
-> foreign key(deptno) references dept_det(deptno));
Query OK, 0 rows affected, 1 warning (4.44 sec)

To insert records:
mysql> insert into emp_det values('Ramu', 'D0002', 8);
Query OK, 1 row affected (0.20 sec)

Inserted records:
mysql> select * from emp_det;
+ + + +
| ename | deptno | hours |
+ + + +
| Ramu | D0002 | 8 |
| Bimu | D0001 | 6 |
| Ramanath | D0003 | 4 |
| Somu | D0002 | 7 |
| Diren | D0004 | 5 |
+ + + +
5 rows in set (0.00 sec)

33
LOKNATH A LAB ON MYSQL
03SU22CC164
Implement the following queries:

a). Display all employee names whose name length is 4 characters.


mysql> select ename from emp_det where length(ename)=4;
+ +
| ename |
+ +
| Ramu |
| Bimu |
| Somu |
+ +
3 rows in set (0.16 sec)

b). Display the department name to which Mr. Ramanath belongs to.
mysql> select dname from dept_det, emp_det where dept_det.deptno=emp_det.deptno and
ename='Ramanath';
+ +
| dname |
+ +
| Chemistry |
+ +
1 row in set (0.05 sec)

c). Display the bonus got by Mr. Ramu.


mysql> select (charge*hours*bonus)/100 "Ramu Bonus" from dept_det, emp_det where
dept_det.deptno=emp_det.deptno and ename='Ramu';
+ +
| Ramu Bonus |
+ +
| 96.0000 |
+ +
1 row in set (0.00 sec)

d). Display the total number of hours taken by Maths department.


mysql> select sum(hours) "Hours by Maths" from dept_det, emp_det where
dept_det.deptno=emp_det.deptno and dname='Maths';
+ +
| Hours by Maths |
+ +
| 15 |
+ +
1 row in set (0.00 sec)

34
LOKNATH A LAB ON MYSQL
03SU22CC164
e). Display different departments.
mysql> select distinct(dname) from dept_det;
+ +
| dname |
+ +
| Computer |
| Maths |
| Chemistry |
| Physics |
+ +
4 rows in set (0.17 sec)

35
LOKNATH A LAB ON MYSQL
03SU22CC164
Q4). Consider the following tables and identify primary and foreign keys,
specify the notnull property for mandatory keys.

STUDENT ( Roll no, Student name, Class, Major)


COURSE ( Course no, Course name, Professor name, Department)
REPORT ( Roll no, Course no, Grade)

To create Student table:


mysql> create table student
-> ( rollno int(6) primary key,
-> studentname varchar(15) not null,
-> class varchar(15),
-> major varchar(15));
Query OK, 0 rows affected, 1 warning (1.06 sec)

To insert records:
mysql> insert into student values('1', ‘Nishad’, 'IBCA', 'Computers');
Query OK, 1 row affected (0.16 sec)

Inserted records:
mysql> select * from student;
+ + + + +
| rollno | studentname | class | major |
+ + + + +
| 1 | Nishad | IBCA | Computers |
| 2 | Shamil | IBCA | AI |
| 3 | Keerthan | IBCA | SD |
| 4 | Sooraj | IBCA | Computers |
| 5 | Gourav | IBCA | CC |
| 6 | Nitesh | IBCA | SD |
| 7 | Sathvik | IBCA | SD |
| 8 | Afraz | IBCA | AI |
| 9 | Fhamee | IBCA | CC |
| 10 | Prithvi | IBCA | Computers |
+ + + + +
10 rows in set (0.00 sec)

36
LOKNATH A LAB ON MYSQL
03SU22CC164
To create Course table:
mysql> create table course
-> (courseno int(6),
-> coursename varchar(15) not null,
-> professorname varchar(15) not null,
-> department varchar(20) not null,
-> foreign key(courseno) references student(rollno));
Query OK, 0 rows affected, 1 warning (0.72 sec)

To insert records:
mysql> insert into course values(001, 'BCA', 'Pramada Basu', 'Computer Science');
Query OK, 1 row affected (0.14 sec)

Inserted records:
mysql> select * from course;
+ + + + +
| courseno | coursename | professorname | department |
+ + + + +
| 1 | BCA | Pramada Basu | Computer Science |
| 2 | BCJ | Raman | Arts |
| 3 | MBA | Sanvi | Management |
| 4 | BBA | Ramesh | Commerce |
| 5 | MCA | Pramada Basu | Computer Science |
| 6 | MBA | Gowtham | Management |
| 7 | BCJ | Raman | Arts |
| 8 | MCA | Pramada Basu | Computer Science |
| 9 | MBA | Sanvi | Management |
| 10 | MBA | Gowtham | Management |
+ + + + +
10 rows in set (0.02 sec)

To create Report table:


mysql> create table report
-> (rollno int(6) references student(rollno),
-> courseno int(6) references course(courseno),
-> grade char(2));
Query OK, 0 rows affected, 2 warnings (0.29 sec)

To insert records:
mysql> insert into report values(001,001,'A');
Query OK, 1 row affected (0.16 sec)

37
LOKNATH A LAB ON MYSQL
03SU22CC164
Inserted records:
mysql> select * from report ;
+ + + +
| rollno | courseno | grade |
+ + + +
| 1| 1|A |
| 2| 2|B |
| 3| 3|A |
| 4| 4|F |
| 5| 5 | A+ |
| 6| 6|A |
| 7| 7|F |
| 8| 8|B |
| 9| 9 | A+ |
| 10 | 10 | B |
+ + + +
10 rows in set (0.00 sec)

38
LOKNATH A LAB ON MYSQL
03SU22CC164
Implement the following queries:
a). Retrieve the names of all students majoring in ‘Computers’.
mysql> select studentname from student where major in ('Computers');
+ +
| studentname |
+ +
| Nishad |
| Sooraj |
| Prithvi |
+ +
3 rows in set (0.08 sec)

b). Retrieve the names of all students and class, who are doing the course taught
by professor ‘Pramada Basu’.
mysql> select studentname,class from student,course where (student.rollno=course.courseno)
and professorname='Pramada Basu';
+ + +
| studentname | class |
+ + +
| Nishad | IBCA |
| Gourav | IBCA |
| Afraz | IBCA |
+ + +
3 rows in set (0.00 sec)

c). Retrieve all departments names who are offering more than one course.
mysql> select department from course group by department having count(*)>1;
+ +
| department |
+ +
| Computer Science |
| Arts |
| Management |
+ +
3 rows in set (0.12 sec)

39
LOKNATH A LAB ON MYSQL
03SU22CC164
d). Retrieve the roll number and names of all students who could not get grade ‘A’ in
any of their course.
mysql> select studentname, rollno from student where rollno in(select rollno from report
where grade not in('A'));
+ + +
| studentname | rollno |
+ + +
| Shamil | 2|
| Sooraj | 4 |
| Gourav | 5|
| Sathvik | 7|
| Afraz | 8|
| Fhamee | 9|
| Prithvi | 10 |
+ + +
7 rows in set (0.14
sec)

40
LOKNATH A LAB ON MYSQL
03SU22CC164
Q5). Create the following tables by identifying primary and foreign keys,
specify the not null property for mandatory keys.
SALES ORDER
COLUMNNAME DATATYPE SIZE CONSTRAINT
Order_no char 6 Primary key First character must be ‘O’, Order_date date,
Clientname varchar 25, Dely_type char 1 Part(P)/ Full (F), Default ‘F’,
Dely_date date cannot be less than Order date.

PRODUCT MASTER
COLUMNNAME DATATYPE SIZE CONSTRAINT
Product_no char 6 Primary key First character must be ‘P’, Descript varchar 20
not null, Qty_on_hand int 8 not null, Cost_price decimal 8,2 cannot be 0,
Sell_price decimal 8,2 cannot be 0.

SALES_ORDER_DETAILS
COLUMNNAME DATATYPE SIZE CONSTRAINT
Order_no char 6 part of the Primary key ; Foreign key – References Order_no
of Sales_order table, Product_no char 6 part of the Primary key; Foreign key-
References Product_no of Product_master table, Qty_ordered int 8,
Qty_supplied int 8, Product_rate decimal 10,2.

41
LOKNATH A LAB ON MYSQL
03SU22CC164
To create Salesorder table:
mysql> create table salesorder
-> (order_no varchar(6) primary key check(order_no like 'O%'),
-> order_date date,
-> clientname varchar(25),
-> dely_type char(1) default 'F',
-> dely_date date,
-> check(dely_type in ('P','F')),
-> constraint dely_date check (dely_date>order_date));
Query OK, 0 rows affected (4.38 sec)

To insert records:
mysql> insert into salesorder values('O0001', '2021-01-20', 'Ivan Bayross', 'F', '2021-12-27');
Query OK, 1 row affected (0.36 sec)

Inserted records:
mysql> select * from salesorder;
+ + + + + +
| order_no | order_date | clientname | dely_type | dely_date |
+ + + + + +
| O0001 | 2021-01-20 | Ivan Bayross | F | 2021-12-27 |
| O0002 | 2022-05-15 | Nishad |P | 2022-11-15 |
| O0003 | 2021-07-10 | Keerthan | P | 2022-01-30 |
| O0004 | 2020-02-28 | Shamil |F | 2021-01-05 |
| O0005 | 2021-10-30 | Ritesh |P | 2021-12-17 |
| O0006 | 2021-01-05 | Gourav |F | 2021-04-25 |
| O0007 | 2022-07-11 | Jaganath | F | 2022-09-10 |
| O0008 | 2023-03-17 | Sathvik |P | 2023-06-28 |
| O0009 | 2022-11-15 | Ivan Bayross | P | 2023-02-28 |
| O0010 | 2021-06-10 | Arjun |P | 2021-09-15 |
+ + + + + +
10 rows in set (0.03 sec)

42
LOKNATH A LAB ON MYSQL
03SU22CC164
To create Product master table:
mysql> create table prod_master
-> (prod_no char(6) primary key check(prod_no like 'P%'),
-> description varchar(20) not null,
-> qty_on_hand int(8) not null,
-> cost_price decimal(8,2),
-> sell_price decimal(8,2),
-> check(cost_price>0),
-> constraint sell_price check(sell_price>0));
Query OK, 0 rows affected, 1 warning (0.34 sec)

To insert records:
mysql> insert into prod_master values('P0001', 'Pendrive', 11, 500, 425);
Query OK, 1 row affected (0.20 sec)

Inserted records:
mysql> select * from prod_master;
+ + + + + +
| prod_no | description | qty_on_hand | cost_price | sell_price |
+ + + + + +
| P0001 | Pendrive | 11 | 500.00 | 425.00 |
| P0002 | CD | 15 | 550.00 | 500.00 |
| P0003 | Monitor | 5 | 3000.00 | 2580.00 |
| P0004 | Keyboard | 10 | 1500.00 | 1400.00 |
| P0005 | Pendrive | 9 | 500.00 | 425.00 |
| P0006 | USB cable | 20 | 250.00 | 200.00 |
| P0007 | Monitor | 10 | 3000.00 | 2580.00 |
| P0008 | Speaker | 15 | 4000.00 | 3700.00 |
| P0009 | Keyboard | 5 | 1500.00 | 1400.00 |
| P0010 | USB cable | 15 | 250.00 | 200.00 |
+ + + + + +
10 rows in set (0.00 sec)

43
LOKNATH A LAB ON MYSQL
03SU22CC164
To create Sales order details table:
mysql> create table sales_ord_det
-> (order_no char(6),
-> prod_no char(6),
-> qtyord int(8),
-> qtysup int(8),
-> prodrate decimal(10,2),
-> foreign key(order_no) references salesorder(order_no),
-> foreign key(Prod_no) references Prod_master(Prod_no));
Query OK, 0 rows affected, 2 warnings (0.49 sec)

To insert records:
mysql> insert into sales_ord_det values('O0001', 'P0001', 24, 19, 225.60);
Query OK, 1 row affected (0.12 sec)

Inserted records:
mysql> select * from sales_ord_det;
+ + + + + +
| order_no | prod_no | qtyord | qtysup | prodrate |
+ + + + + +
| O0001 | P0001 | 24 | 19 | 225.60 |
| O0002 | P0001 | 20 | 15 | 500.24 |
| O0003 | P0005 | 10 | 9 | 455.50 |
| O0004 | P0002 | 11 | 11 | 575.25 |
| O0005 | P0008 | 20 | 14 | 710.75 |
| O0006 | P0009 | 15 | 13 | 500.24 |
| O0007 | P0010 | 15 | 15 | 2590.10 |
| O0008 | P0007 | 5 | 3 | 3500.10 |
| O0009 | P0007 | 15 | 9 | 400.10 |
| O0010 | P0010 | 10 | 5 | 2590.10 |
+ + + + + +
10 rows in set (0.00 sec)

44
LOKNATH A LAB ON MYSQL
03SU22CC164
Implement the following queries:
a). Display the order_date in the format ‘DD-Month-YY’.
mysql> select date_format(order_date, '%d %M %Y') "Order Date" from salesorder;
+ +
| Order Date |
+ +
| 20 January 2021 |
| 15 May 2022 |
| 10 July 2021 |
| 28 February 2020 |
| 30 October 2021 |
| 05 January 2021 |
| 11 July 2022 |
| 17 March 2023 |
| 15 November 2022 |
| 10 June 2021 |
+ +
10 rows in set (0.00 sec)

b). Print the information from Sales_order_details table for orders place in the
month of ‘January’.
mysql> select * from salesorder where date_format(order_date, '%M')='January';
+ + + + + +
| order_no | order_date | clientname | dely_type | dely_date |
+ + + + + +
| O0001 | 2021-01-20 | Ivan Bayross | F | 2021-12-27 |
| O0006 | 2021-01-05 | Gourav |F | 2021-04-25 |
+ + + + + +
2 rows in set (0.00 sec)

c). Find the product whose selling_price is greater than 2000 and less than or equal
to 5000.
mysql> select prod_no from prod_master where sell_price>2000 and sell_price<=5000;
+ +
| prod_no |
+ +
| P0003 |
| P0007 |
| P0008 |
+ +
3 rows in set (0.04 sec)

45
LOKNATH A LAB ON MYSQL
03SU22CC164
d). Find the Product_no and description of non-moving products i.e., products not
being sold.
mysql> select Prod_no,description from prod_master where prod_no not in(select Prod_no
from Sales_ord_det);
+ + +
| Prod_no | description |
+ + +
| P0003 | Monitor |
| P0004 | Keyboard |
| P0006 | USB cable |
+ + +
3 rows in set (0.00
sec)

e). Find the products and their quantities for the orders placed by client ‘Ivan
Bayross’. mysql> select qtyord, description from sales_ord_det,prod_master, salesorder
where sales_ord_det.prod_no=prod_master.prod_no and
sales_ord_det.order_no=salesorder.order_no and clientname='Ivan Bayross';
+ + +
| qtyord | description |
+ + +
| 24 | Pendrive |
| 15 | Monitor |
+ + +
2 rows in set (0.00
sec)

46

You might also like