DATABASE SYSTEMS
LAB MANUAL
1
DEPARTMET
OF
Certified that Mr. /Mrs. /Mis_____________________________
of ________________________________class has carried out the
necessary Practical work as per courses of studies by ______________
of for the year as shown in
the practical schedule of this journal.
PROFFESOR INCHARGE
DEPARTMENT OF ________________
DATE ___________________________
2
List of Practical’s
Practical No /
Query No
Title Page no
0 Installation of Visual Studio for Execution of SQL Queries 4
1 Create a new database in server 7
2 Create a new table in database 8
3 Display overall table 9
4 Insert only single record 10
5 Display overall table with insert only one record 11
6 Check primary key 12
7 Insert multiple records 13
8 Display overall table 15
9 Record updated 16
10 Display overall table 17
11 Delete specific records 18
12 Display table after deleting records 19
13 Display specific fields from table 20
14 Display specific fields with conditions (Where Clause) 21
15 Display specific records with conditions (Where Clause) 22
16 Display specific records with multiple conditions 23
17 Sorting the Data 24
18 Alter the table (Add new field) 25
19 Insert / Update data in alter field 26
20 Display table after update data in alter field 27
21 Perform Arithmetic operations for calculation 28
22 Use IN operator 29
23 Use of Between operator 30
24 Use of Between operator with AND, OR 31
25 Use of NOT Between operator 32
3
Practical No Title Page no
26 Use like (%) operator 33
27 Use like (_____ / Underscore) operator 34
28 Sum of SALARY 35
29 Sum functions with where clause/ 36
30 Use Count function 37
31 Use Count function with Where Clause 38
32 Use Count function on specific field 39
33 Average function on SALARY 40
34 MAX (Maximum) function 41
35 MIN (Minimum) function 42
36 Group by Clause 43
37 Perform all Functions 44
38 Create 2nd Table 45
39 Display 2nd Table 46
40 Insert data in 2nd Table 47
41 Display 2nd Table after insert data 48
42 INNER Joining 49
43 Joining Tables 50
44 Full OUTER join with Where clause 51
45 Update Table of Employee_mngm 52
46 Display table of Employee_mngm 53
47 RIGHT Joining 54
48 Update Table of BOARD_DIR 55
49 Display Table of BOARD_DIR 56
50 RIGHT Joining 57
51 NULL operator 58
4
Practical # 0
Installation of Visual Studio for Execution of SQL Queries
Many numbers of tools are available in Internet for Executing of SQL queries like:
SQL server management studio
Visual Studio etc
But we use a Visual Studio 2022 for SQL Queries
We Install the Visual Studio Community Version and Install it in Computer / Laptop
and the open the Visual Studio and Click the Continue without code. (See figure 0.1)
Continue without code
Figure # 0.1
After click the continue without code the interface of visual studio will appeared. (See figure 0.2)
Figure # 0.2
5
We click File option on Right top side and then click new then click new. (See figure 0.3)
Figure # 0.3
After new file that the new window will display and click sql file option then click open . (See figure 0.4)
Open
SQL File
Figure # 0.4
This is a interface of visual studio for sql . (See figure 0.5)
Open
6
Figure # 0.5
Now we click connect to the server and then new window will appeared and we select server and
database then click the connect. (See figure 0.6)
Connect to Server Serve
r
Connect
Database
Figure # 0.6
Now this is interface for sql. we will create and execute the queries of sql. (See figure 0.7)
Figure # 0.7
Page 7 of 63
Query # 1
Create a new database in server
Query:
Create DataBase Practicals;
Output:
Page 8 of 63
Query # 2
Create a new table in database
Query:
Create Table Employee_mngm(EMP_ID int not null, EMP_NAME Varchar(30) not null, EMP_FATHER_NAME Varchar(30) not null,
EMP_AGE int not null, EMP_ADDRESS Varchar(50) not null, EMP_PHONE Varchar(13) not null, DEPARTMENT
Varchar(25) not null, POST Varchar(25) not null, SALARY int not null, primary key(EMP_ID , EMP_PHONE));
Output:
Page 9 of 63
Query # 3
Display Overall Table
Query:
Select * from Employee_mngm;
Output:
Page 10 of 63
Query # 4
Insert only single record
Query:
Insert into Employee_mngm (EMP_ID, EMP_NAME, EMP_FATHER_NAME, EMP_AGE, EMP_ADDRESS, EMP_PHONE,
DEPARTMENT, POST, SALARY)
Values(7275001, 'Muhammad Amjad', 'Muhammad Khan', 26, 'Bahria town Karachi', '+923009876543', 'Security', 'Employee', 23000);
Output:
Page 11 of 63
Query # 5
Display overall table with insert only one record
Query:
Select * from Employee_mngm;
Output:
Page 12 of 63
Query # 6
Check Primary key
Query:
Insert into Employee_mngm(EMP_ID, EMP_NAME, EMP_FATHER_NAME, EMP_AGE, EMP_ADDRESS, EMP_PHONE,
DEPARTMENT, POST, SALARY)
Values(7275701, 'Ahad Ali', 'Ali Raza', 23, 'Multan', '+923049876643', 'Stock', 'Manager', 35000);
Page 13 of 63
Output:
Query # 7
Insert Multiple records
Query:
Insert into Employee_mngm(EMP_ID, EMP_NAME, EMP_FATHER_NAME, EMP_AGE, EMP_ADDRESS, EMP_PHONE,
DEPARTMENT, POST, SALARY)
Page 14 of 63
Values(7275103, 'Ahad Ali', 'Ali Raza', 23, 'Multan', '+923009876543', 'Stock', 'Manager', 35000),
(7275101, 'Khalid', 'Muhammad Amin', 24, 'Karachi', '+923002756541', 'Stock', 'Employee', 21000),
(7275702, 'Muhammad Ahsan', 'Muhammad Khan', 27, 'Islamabd', '+923029878441', 'Finance', 'Junior manager', 30000),
(7275002, 'Muhammad Saleem', 'Tahir', 26, 'Lahore', '+923306216308', 'Security', 'Junior Manager', 32000),
(7275003, 'Waleed', 'Khan', 32, 'Faisalabad', '+923100879653', 'Security', 'Manager', 38000),
(7275701, 'Muhammad Amjad', 'Muhammad Iqbal', 25, 'Faisalabad', '+923074829041', 'Finance', 'Employee', 26000),
(7275501, 'Saqib', 'Ali', 22, 'Peshaawar', '+923409774540', 'Client Care', 'Employee', 28000),
(7275503, 'Kashif', 'Ali', 27, 'Karachi', '+923099876513', 'Client Care', 'Manager', 42000),
(7275102, 'Muhammad Amir', 'Muhammad Liyaqat', 24, 'Multan', '+923019776447', 'Stock', 'Junior Manager', 28000),
(7275703, 'Muhammad Liyaqat', 'Ali', 25, 'Islamabad', '+923109476040', 'Finance', 'Manager', 34000),
(7275502, 'Muhammad Murtaza', 'Muhammad Ali', 28, 'Lahore', '+923309176041', 'Client Care', 'Junior Manager', 36000);
Output:
Page 15 of 63
Query # 8
Page 16 of 63
Display Overall Table
Query:
Select * from Employee_mngm;
Output:
Page 17 of 63
Query # 9
Record Updated
Query:
Update Employee_mngm Set EMP_PHONE = '+923045385698' where EMP_ID = 7275103;
Page 18 of 63
Output:
Query # 10
Display Overall Table
Query:
Page 19 of 63
Select * from Employee_mngm;
Output:
Query # 11
Delete a specific records
Page 20 of 63
Query:
Delete Employee_mngm where DEPARTMENT = 'Stock' AND POST = 'Manager' ;
Delete Employee_mngm where DEPARTMENT = 'Client Care' AND POST = 'Employee' ;
Output:
Query # 12
Page 21 of 63
Display table after deleting records
Query:
Select * from Employee_mngm;
Output:
Page 22 of 63
Query # 13
Display specific fields from table
Query:
Select EMP_ID, POST, DEPARTMENT, SALARY from Employee_mngm;
Output:
Page 23 of 63
Query # 14
Display specific fields with condition (Where Clause) from table
Query:
Select EMP_ID, POST, DEPARTMENT, SALARY from Employee_mngm where SALARY >= 35000;
Output:
Page 24 of 63
Query # 15
Display specific records with condition (Where Clause) from table
Query:
Select * from Employee_mngm where POST = ‘Manager’;
Output:
Page 25 of 63
Query # 16
Display specific records with multiple conditions (Where Clauses) from table
Query:
Select * from Employee_mngm where EMP_AGE > 25 AND EMP_ADDRESS = ‘Lahore’;
Output:
Page 26 of 63
Query # 17
Sorting the Data
Query:
Select * from Employee_mngm order by EMP_AGE DESC;
Output:
Page 27 of 63
Query # 18
Alter the table (Add new field)
Query:
ALTER TABLE Employee_mngm ADD Bonus int;
Output:
Page 28 of 63
Query # 19
Insert/Update data in Alter field
Query:
Update Employee_mngm Set Bonus = (SALARY * 5) / 100 where DEPARTMENT = 'Security';
Update Employee_mngm Set Bonus = (SALARY * 5) / 100 where DEPARTMENT = 'Stock';
Update Employee_mngm Set Bonus = (SALARY * 5) / 100 where DEPARTMENT = 'Client Care';
Update Employee_mngm Set Bonus = (SALARY * 5) / 100 where DEPARTMENT = 'Finance';
Output:
Page 29 of 63
Query # 20
Display table after update data in Alter field
Query:
Select * from Employee_mngm;
Output:
Page 30 of 63
Query # 21
Perform Arithmetic operations for calculations
Query:
Select EMP_ID, EMP_NAME, DEPARTMENT, POST, SALARY, (SALARY*12) + (BONUS*12) from Employee_mngm;
Output:
Page 31 of 63
Query # 22
Use of IN operator
Query:
Select EMP_ID, EMP_NAME, DEPARTMENT from Employee_mngm where Post in('Manager');
Output:
Page 32 of 63
Query # 23
Use of Between operator
Query:
Select EMP_ID , EMP_NAME , POST , DEPARTMENT , SALARY from Employee_mngm where SALARY
between 33000 and 44000;
Output:
Page 33 of 63
Query # 24
Use of Between operator with AND , OR
Query:
Select EMP_ID , EMP_NAME , POST , DEPARTMENT , SALARY from Employee_mngm where SALARY
between 33000 AND 40000 OR POST = ‘Manager’;
Output:
Page 34 of 63
Query # 25
Use of NOT Between operator
Query:
Select EMP_ID , EMP_NAME , POST , DEPARTMENT , SALARY from Employee_mngm where SALARY Not
between 33000 AND 41000 AND POST = ‘Junior Manager’;
Output:
Page 35 of 63
Query # 26
Use like (%) operator
Query:
Select EMP_NAME from Employee_mngm where EMP_NAME like ‘%D’;
Output:
Page 36 of 63
Query # 27
Use like (_____ / Underscore) operator
Query:
Select EMP_NAME from Employee_mngm where EMP_NAME like ‘_____’;
Output:
Page 37 of 63
Query # 28
Sum of SALARY
Query:
Select SUM(SALARY) from Employee_mngm;
Output:
Page 38 of 63
Query # 29
Sum function with Where Clause
Query:
Select SUM(SALARY) from Employee_mngm Where SALARY >= 30000;
Output:
Page 39 of 63
Query # 30
Use Count Function
Query:
Select Count(*) from Employee_mngm;
Output:
Page 40 of 63
Query # 31
Use Count Function with Where Clause
Query:
Select Count(*) from Employee_mngm Where SALARY >= 33000;
Output:
Page 41 of 63
Query # 32
Use Count Function on specific field
Query:
Select Count(SALARY) from Employee_mngm;
Page 42 of 63
Output:
Query # 33
Average Function on SALARY
Query:
Select AVG(SALARY) from Employee_mngm;
Page 43 of 63
Output:
Query # 34
MAX (Maximum) function
Query:
Select MAX(SALARY) from Employee_mngm;
Page 44 of 63
Output:
Query # 35
MIN (Minimum) function
Query:
Page 45 of 63
Select MIN(SALARY) from Employee_mngm;
Output:
Query # 36
Group by Clause
Query:
Page 46 of 63
Select SUM(SALARY) , AVG(SALARY) from Employee_mngm group by DEPARTMENT;
Output:
Query # 37
Perform All Functions
Query:
Page 47 of 63
Select Count(SALARY) , SUM(SALARY) , AVG(SALARY) , MAX(SALARY) , MIN(SALARY) from
Employee_mngm group by DEPARTMENT;
Output:
Query # 38
Create 2nd Table
Query:
Page 48 of 63
Create Table BOARD_DIR(DIR_ID Varchar(20) not null, DIR_NAME Varchar(30) not null, DEAPRTMENT
Varchar(60) not null, primary key(DIR_ID , DEPARTMENT));
Output:
Query # 39
Display 2nd Table
Query:
Page 49 of 63
Select * from BOARD_DIR;
Output:
Query # 40
Insert data in 2nd Table
Query:
Page 50 of 63
Insert into BOARD_DIR(DIR_ID, DIR_NAME, DEPARTMENT)
Values(‘SCU_5401, 'ARIF', 'SECURITY'), (‘STC_3019, 'Hammad', 'STOCK'),
(‘CLNT_9013, 'IMRAN', 'CLIENT CARE'), (‘FINC_2703, 'BABAR', 'FINANCE');
Output:
Query # 41
Display 2nd Table after insert data
Query:
Page 51 of 63
Select * from BOARD_DIR;
Output:
Query # 42
Perform INNER Joining
Query:
Page 52 of 63
SELECT Employee_mngm.EMP_ID, Employee_mngm.EMP_NAME , BOARD_DIR.ID, BOARD_DIR.DEPARTMENT
FROM Employee_mngm
INNER JOIN BOARD_DIR
ON Employee_mngm.DEPARTMENT = BOARD_DIR.DEPARTMENT;
Output:
Query # 43
Joining Tables
Page 53 of 63
Query:
SELECT * from Employee_mngm, BOARD_DIR
Where Employee_mngm.DEPARTMENT = BOARD_DIR.DEPARTMENT;
Output:
Query # 44
Full Outer Join with Where Clause
Page 54 of 63
Query:
SELECT Employee_mngm.* , BOARD_DIR.*
FROM BOARD_DIR
FULL OUTER JOIN Employee_mngm
ON Employee_mngm.DEPARTMENT = BOARD_DIR.DEPARTMENT
where SALARY Between 30000 AND 40000;
Output:
Query # 45
Page 55 of 63
Update Table of Employee_mngm
Query:
Update Employee_mngm Set DEPARTMENT = ‘ ’ Where EMP_ID = 7275502;
Output:
Query # 46
Page 56 of 63
Display Table
Query:
Select * from Employee_mngm;
Output:
Query # 47
Page 57 of 63
Right Joining
Query:
SELECT Employee_mngm.* , BOARD_DIR.DIR_ID , BOARD_DIR.DEPARTMENT
FROM Employee_mngm
RIGHT JOIN BOARD_DIR
ON Employee_mngm.DEPARTMENT = BOARD_DIR.DEPARTMENT
Order by SALARY ASC ;
Output:
Page 58 of 63
Query # 48
Update table of BOARD_DIR
Query:
Update BOARD_DIR Set DEPARTMENT = ' ' Where DIR_ID = ‘FINC_2703’;
Update BOARD_DIR Set DEPARTMENT = ' ' Where DIR_ID = ‘STC_3019’;
Output:
Page 59 of 63
Query # 49
Display Table
Query:
Select * from BOARD_DIR;
Output:
Page 60 of 63
Query # 50
Right Joining
Query:
SELECT Employee_mngm.* , BOARD_DIR.DIR_ID , BOARD_DIR.DEPARTMENT
FROM Employee_mngm
LEFT JOIN BOARD_DIR
ON Employee_mngm.DEPARTMENT = BOARD_DIR.DEPARTMENT
Order by SALARY ASC ;
Page 61 of 63
Output:
Query # 51
NULL Operator
Query:
Select EMP_ID , EMP_NAME from Employee_mngm
where SALARY is NOT Null;
Page 62 of 63
Output:
Page 63 of 63