Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

PRACTICAL - 1

Aim: To understand the basic concept of RDBMS.


SQL: Structured Query Language is a standard language for dealing with relational databases.
It is a query language for storing, manipulating and retrieving data stored in relational
databases.
Characteristics of SQL:
1. It is easy to learn.
2. It is used to access the data from relational databases.
3. It can execute queries such as creating, manipulating the table.
4. It also allows the user to set permissions on the table.
Advantages of SQL:
1. High speed: Users can quickly and effectively retrieve a large amount of records from
the database.
2. No coding required: It doesn't require the coding to manage the database.
3. Well defined standard: The sql database standards are defined by ISO and ANSI.
4. Portability: It can be used in laptop/ pc or even mobiles.
5. Interactive: It is a domain language used to communicate to a database.
6. Multiple Dataview: Users can make different views of the database structure.
Rows are known as tuples
Columns are known as attributes
Examples of RDBMS
● Oracle
● mySQL
● MS Acess
● IBM
Types of SQL statements
1. DML: It includes the retriever, insertion, deletion and modification of data.
Commands:
1) Insert- Create a new record.
2) Update- Modifies the record.
3) Delete- Deletes the record.
2. DDL: Used by designers to define the schemas
Commands:
1) Create- Creates a table.
2) Alter- Modifies existing table.
3) Drop- Deletes the table.
3. DCL: used to control access to the data stored in the database.
Commands:.
1) Grant- Giving privilege to user.
2) Revoke- Taking permission back from user.
4. DQL: Retrieving data from database.
Commands:
1) Select- Retrieving data from database.
5. TCL: helpful in carrying out transactions.
Commands:
1) Commit- Used to save data permanently.
2) Rollback- Get or restore data to the last savepoint.
3) Savepoint- Save data to a particular point temporarily.
Terms used in Relational Database
1. Table/Relation: Collection of column and row.
2. Record or Tuple: Row known as tuple/record.
3. Column or Attribute: Column name specifies vertical entity wrt which information
needs to be stored.
4. Domain: It is a set of permitted values for an attribute in the table.
5. Instants or Schema: It describes the structure of a table at any instant of time.
6. Key: Represents a unique identifier for any relation.
7. Oracle: One of most prominent rdbms which was developed in 1977.
PRACTICAL - 2
Aim: To perform basic SQL commands.
Data Types:
1. String:
a. Char: Stores string values containing any character in a character set.
b. Varchar: Stores string values containing any character in a character set but of
varying length.
c. BLOB/CLOB: It stores binary strings in hexadecimal format.
2. Number:
a. Int: It stores exact numbers with a predefined precision.
3. Temporal:
a. Timestamp: It is used to store a timestamp value.
4. Boolean:
a. Boolean: Stores true and false values.
Commands:
1. Create:
Purpose:
The create table command is used to create a new table in the database.
Syntax:
Create table tablename(
Column_name datatype(size),
Column_name datatype(size),
Column_name datatype(size));
2. Insert into:
Purpose:
The insert into command in SQL is used to insert a new row into a table.
Syntax:
Insert into Table_name values(value1, value2, value3);
3. Select:
Purpose:
The select command is used to retrieve the data from the database.
Syntax:
Select * from Table_name;
Select column1, column2 from Table-name;
Create a table name client with client no. ,name, address, city, pincode, state and balance
due:
Input:
Create table Client_ (
Client_No char(5),
Name_ char(20),
Address_1 char(20),
Address_2 char(20),
City char(20),
Pincode int,
State char(20),
Bill_Due int);
insert into Client_ values('C0001', 'Ajay', 'Thomas Street', 'Powari', 'Mumbai', 122004,
'Maharashtra', 1500);
insert into Client_ values('C0002', 'Ram', 'Vigyan Street', 'Pukter', 'Jaipur', 122011, 'Rajasthan',
1600);
insert into Client_ values('C0003', 'Anil', 'Dayal Street', 'Rohini', 'Delhi', 123002, 'Delhi',
2000);
insert into Client_ values('C0004', 'Rashmi', 'Golf Course', 'Extension', 'Gurgaon', 122001,
'Haryana', 4000);
insert into Client_ values('C0005', 'Ankit', 'Auring Street', 'Shastri Nagar', 'Meerut', 122051,
'Uttar Pradesh', 4500);
select * from Client_;

Output:

CLIENT_NO NAME_ ADDRESS_1 ADDRESS_2 CITY PINCODE STATE BILL_DUE

C0001 Ajay Thomas Powari Mumbai 122004 Maharashtra 1500


Street
C0002 Ram Vigyan Pukter Jaipur 122011 Rajasthan 1600
Street
C0003 Anil Dayal Street Rohini Delhi 123002 Delhi 2000
C0004 Rashmi Golf Course Extension Gurgaon 122001 Haryana 4000
C0005 Ankit Auring Shastri Meerut 122051 Uttar Pradesh 4500
Street Nagar
PRACTICAL - 3
Aim: To understand the basics of SQL operators (AND, OR, NOT).
Theory:
AND: The SQL AND operator displays a record if all the conditions separated are true.
Syntax:
Select column1, column2
from tablename
where condition1 AND condition2;
OR: The SQL OR operator displays the record if any of the conditions separated by or is
true.
Syntax:
Select column1, column2
from tablename
where condition1 OR condition2;

NOT: The SQL NOT operator displays the record if any of the condition is not true.
Syntax:
Select column1, column2
from tablename
where condition1 NOT condition2;
Create a table name Customer and perform queries:
Input:
Create table Customer(
CustomerID int,
Customer_Name char(20),
Contact_Name char(20),
Address char(40),
City char(40),
Postal_Code char(20),
Country char(40));
insert into Customer values (1,'alferds futterkiste','maria anders','obere
str.57','berlin',12209,'germany');
insert into Customer values (2,'ana trujillo ','ana trujillo','avda. de la constitucion
2222','mexico D.F.',05021,'mexico');
insert into Customer values (3,'antonio taqueria','antonio moreno','mataderos 2312','mexico
D.F.',05023,'mexico');
insert into Customer values (4,'around the horn','thomas hardy','120 hanover
sq.','london','WA1 1DP','UK');
insert into Customer values (5,'berglunds snabbkop','christina berglund','berguvsvagen
8','lulea','S-958 22','sweden');
insert into Customer values (6,'blauer see ','hanna
moos','forsterstr,57','mannheim',68306,'germany');
insert into Customer values (7,'blondel pere et fils','frederique citeaux','24,place
kleber','strasbourg',67000,'france');
insert into Customer values (8,'bolido comidas','martin sommer','c/
araquil,67','madrid',28023,'spain');
select * from Customer;
Output:

CUSTOMERID CUSTOMER_NAME CONTACT_NAME ADDRESS CITY POSTAL_CODE COUNTRY

1 alferds maria anders obere str.57 berlin 12209 germany


futterkiste

2 ana trujillo ana trujillo avda. de la mexico D.F. 5021 mexico


constitucion

3 antonio antonio moreno mataderos 2312 mexico D.F. 5023 mexico


taqueria

4 around the horn thomas hardy 120 hanover sq. london WA1 1DP UK

5 berglunds christina berguvsvagen 8 lulea S-958 22 sweden


snabbkop berglund

6 blondel pere et frederique 24,place kleber strasbourg 67000 france


fils citeaux

7 bolido comidas martin sommer c/ araquil,67 madrid 28023 spain

Q1. Select record from table where the country is germany and city is berlin.
select* from customer where city='berlin' AND country='germany';
CUSTOMERID CUSTOMER_NAME CONTACT_NAME ADDRESS CITY POSTAL_CODE COUNTRY

1 alferds maria anders obere str.57 berlin 12209 germany


futterkiste

Q2. You have to retrieve the record from customer table where city is berlin or city is
merchant.
select *from customer where city='berlin' OR city='merchant';
CUSTOMERID CUSTOMER_NAME CONTACT_NAME ADDRESS CITY POSTAL_CODE COUNTRY

1 alferds maria anders obere berlin 12209 germany


futterkiste str.57

Q3. Retrieve the record where country is not equal to germany.


select *from customer where NOT country='germany';
CUSTOMERID CUSTOMER_NAME CONTACT_NAME ADDRESS CITY POSTAL_CODE COUNTRY

2 ana trujillo ana trujillo avda. de la mexico D.F. 5021 mexico


constitucion
3 antonio antonio mataderos mexico D.F. 5023 mexico
taqueria moreno 2312
4 around the thomas hardy 120 hanover london WA1 1DP UK
horn sq.
5 berglunds christina berguvsvagen lulea S-958 22 sweden
snabbkop berglund 8
6 blondel pere frederique 24,place strasbourg 67000 france
et fils citeaux kleber
7 bolido martin c/ madrid 28023 spain
comidas sommer araquil,67

Q4. Select the record from costumer table where country is germany and city is
berlin or city is merchant.
select* from customer where country='germany' AND city='berlin' OR
city='merchant';
CUSTOMERID CUSTOMER_NAME CONTACT_NAME ADDRESS CITY POSTAL_CODE COUNTRY

1 alferds maria anders obere berlin 12209 germany


futterkiste str.57
PRACTICAL - 4
Aim: To understand basic SQL operators (AND, OR).
Create a table and perform queries.
Input:
Create table instructor(
ID_ int,
Name_ char(21),
Deptname char(30),
Salary char(20));
Insert into instructor values (10101,'srinivas','mechanical department',75000);
Insert into instructor values (10102,'Arjun','Physics department',92000);
Insert into instructor values (10103,'ram','Chemistry department',80000);
Insert into instructor values (10104,'Shyam','chemistry department',95000);
Insert into instructor values (10105,'ashu','Physics department',98000);
Insert into instructor values (10106,'sakshi','Physics department',97000);
select*from instructor;
Output:

ID_ NAME_ DEPTNAME SALARY

10101 srinivas mechanical department 75000


10102 Arjun Physics department 92000
10103 ram Chemistry department 80000
10104 Shyam chemistry department 95000
10105 ashu Physics department 98000
10106 sakshi Physics department 97000

Q1. Find all the instructor with salary greater than 90,000.
select Name_ from instructor where salary> 90000;
NAME_

Arjun
Shyam
ashu
sakshi

Q2. Find an instructor in the Physics Department with Salary greater than 90,000.
select Name_ from instructor where deptname = 'Physics department' and salary>
90000;
NAME_

Arjun
ashu
sakshi

Q3. Select the instructor whose department name is mechanical department.


select Name_ from instructor where deptname = 'mechanical department';
NAME_

srinivas
PRACTICAL - 5
Aim: To perform the basic SQL commands(ORDER BY, DISTINCT).
Theory:
ORDER BY: The ORDER BY keyword is used to sort the result-set in ascending or
descending order.
Syntax:
Select column list
from table_name
where condition
ORDER BY column1,...., column n;
DISTINCT: The select DISTINCT command is used to return only the distinct values which
means different values. In order to remove duplicate values we apply the distinct command.
Syntax:
Select DISTINCT column1,column2
from table_name;
Create a table with the name employee with attributes ID, Name, Address, Age and
Salary. And solve the query using order by and distinct function.
Input:
Create table Employee (
ID_ int,
Name_ char(20),
Age int,
Address char(20),
Salary int);
insert into Employee values(1, 'Ramesh', 32, 'Ahmedabad', 2000);
insert into Employee values(2, 'Khilan', 25, 'Delhi', 1500);
insert into Employee values(3, 'Kaushik', 23, 'Kota', 2000);
insert into Employee values(4, 'Chitali', 25, 'Mumbai', 6500);
insert into Employee values(5, 'Hardik', 27, 'Bhopal', 8500);
insert into Employee values(6, 'Komal', 22, 'MP', 4500);
insert into Employee values(7, 'Muffy', 24, 'Indore', 9000);
insert into Employee values(8, 'Muffy', 23, 'Chennai', 2100);
select * from Employee;
Output:

ID_ NAME_ AGE ADDRESS SALARY

1 Ramesh 32 Ahmedabad 2000

2 Khilan 25 Delhi 1500

3 Kaushik 23 Kota 2000

4 Chitali 25 Mumbai 6500

5 Hardik 27 Bhopal 8500

6 Komal 22 MP 4500

7 Muffy 24 Indore 9000

8 Muffy 23 Chennai 2100


Q1. Apply Order By (Salary)
select * from Employee order by Salary;

ID_ NAME_ AGE ADDRESS SALARY

2 Khilan 25 Delhi 1500


1 Ramesh 32 Ahmedabad 2000
3 Kaushik 23 Kota 2000
8 Muffy 23 Chennai 2100
6 Komal 22 MP 4500
4 Chitali 25 Mumbai 6500
5 Hardik 27 Bhopal 8500
7 Muffy 24 Indore 9000

Q2. Apply Distinct command on Name_


select distinct Name_ from Employee;

NAME_

Khilan
Chitali
Komal
Hardik
Ramesh
Kaushik
Muffy

Q3. Display all information from Employee table who live in Indore
select * from Employee where Address = 'Indore';

ID_ NAME_ AGE ADDRESS SALARY

7 Muffy 24 Indore 9000

Q4. Find the Employee Id_ and Name_ whose Salary > 2000
select ID_, Name_ from Employee where Salary > 2000;

ID_ NAME_

4 Chitali

5 Hardik

6 Komal

7 Muffy

8 Muffy

Q5. Retrieve the name of the Employee = Ramesh or name = Khilan


select Name_ from Employee where Name_ = 'Ramesh' or Name_ = 'Khilan';

NAME_

Ramesh
Khilan

Q6. Retrieve the name of Employee where address is not equal to Bhopal
select Name_ from Employee where Address <> 'Bhopal';

NAME_

Ramesh
Khilan
Kaushik
Chitali
Komal
Muffy
Muffy
PRACTICAL - 6
Aim: To implement the concept of joins in SQL .
Theory:
A join clause is used to combine rows from two or more tables, based on a related column
between them.
The different types of the JOINs in SQL:
Inner Join: Returns records that have matching values in both tables.
Syntax:
Select column_name(s)
from table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

Left outer Join: Returns all records from the left table, and the matched records from the
right table.
Syntax:
Select column_name(s)
from table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

Right outer Join: Returns all records from the right table, and the matched records from the
left table.
Syntax:
Select column_name(s)
from table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Create 4 tables named order1, order2, customer1, customer2 with their respective
attributes and perform inner, left and right join:
Input:
create table order1(
OrderID int,
CustomerID int,
OrderDate char(20));
insert into order1 values(10308,2,'18-9-21');
insert into order1 values(10309,37,'21-10-21');
insert into order1 values(10310,77,'01-01-21');
select * from order1;
create table customer1(
CustomerID int,
CustomerName char(20),
ContactName char(20),
Country char(20));

insert into customer1 values(1,'Alex','Maria','Germany');


insert into customer1 values(2,'Friedo','Helen','Mexico');
insert into customer1 values(3,'Thomas','Rose','Mexico');
select * from customer1;

create table customer2(


ID_ int,
CustomerName char(20),
Age int,
Address char(20),
Salary int);

insert into customer2 values(1,'Ramesh',20,'Ahemdabad',2000);


insert into customer2 values(2,'Khitan',21,'Delhi',3000);
insert into customer2 values(3,'Kaushik',24,'Bhopal',3000);
insert into customer2 values(4,'Chaitali',25,'Indore',5000);
insert into customer2 values(5,'Hardik',26,'Delhi',6000);
insert into customer2 values(6,'Muffy',21,'Gurugram',4000);
select * from customer2;

create table order2(


Order_ID int,
OrderDate char(20),
C_ID int,
Amount int);

insert into order2 values(102,'14-1-21',3,3000);


insert into order2 values(100,'15-02-22',3,1500);
insert into order2 values(101,'16-05-21',2,1500);
insert into order2 values(103,'14-06-21',4,1900);
select * from order2;
Output:
Order1
ORDERID CUSTOMERID ORDERDATE

10308 2 18-9-21

10309 37 21-10-21

10310 77 01-01-21

Customer1
CUSTOMERID CUSTOMERNAME CONTACTNAME COUNTRY

1 Alex Maria Germany

2 Friedo Helen Mexico

3 Thomas Rose Mexico

Q1. Perform inner join on the given tables


select * from order1 INNER JOIN customer1 ON order1.CustomerID =
customer1.CustomerID;

ORDERID CUSTOMERID ORDERDATE CUSTOMERID CUSTOMERNAME CONTACTNAME COUNTRY

10308 2 18-9-21 2 Friedo Helen Mexico

Q2. Perform Right join on the given tables


select * from order1 RIGHT JOIN customer1 ON order1.CustomerID =
customer1.CustomerID;

ORDERID CUSTOMERID ORDERDATE CUSTOMERID CUSTOMERNAME CONTACTNAME COUNTRY

10308 2 18-9-21 2 Friedo Helen Mexico

- - - 1 Alex Maria Germany

- - - 3 Thomas Rose Mexico

Q3. Perform Left Join on the given tables.


select * from order1 LEFT JOIN customer1 ON order1.CustomerID =
customer1.CustomerID;

ORDERID CUSTOMERID ORDERDATE CUSTOMERID CUSTOMERNAME CONTACTNAME COUNTRY

10308 2 18-9-21 2 Friedo Helen Mexico


10309 37 21-10-21 - - - -
10310 77 01-01-21 - - - -

Q4. Perform Full join on the given tables.


select * from order1 FULL JOIN customer1 ON order1.CustomerID =
customer1.CustomerID;

ORDERID CUSTOMERID ORDERDATE CUSTOMERID CUSTOMERNAME CONTACTNAME COUNTRY

- - - 1 Alex Maria Germany

10308 2 18-9-21 2 Friedo Helen Mexico

- - - 3 Thomas Rose Mexico

10309 37 21-10-21 - - - -

10310 77 01-01-21 - - - -
PRACTICAL - 7
Aim: To implement the concept of joins in SQL .
Create two tables name employee and projects and apply join queries on both the tables.
Input:
create table employee (
EmpID int,
EmpFName char(20),
EmpLName char(20),
Age int,
PhoneNo int,
Address char(20));
insert into employee values (1,'Vardhan','Kumar',22,9876543421,'Delhi');
insert into employee values (2,'Himani','Sharma',32,8777384798,'Mumbai');
insert into employee values (3,'Aayush','Sharma',24,8787878787,'Kolkata');
insert into employee values(4,'Hemsmith','Sharma',25,1314516167,'Bengaluru');
insert into employee values (5,'Sewathi','Kapoor',26,7676767676,'Hyderabad');
Select * from employee;

create table projects(


ProjectID int,
EmpID int,
ClientID int,
ProjectName char(20));
insert into projects values(111, 4 ,3, 'P1');
insert into projects values(222, 2, 1, 'P2');
insert into projects values(333, 3, 5, 'P3');
insert into projects values(444, 3, 2, 'P4');
select * from projects ;
Output:

EMPID EMPFNAME EMPNAME AGE PHONENO ADDRESS

1 Vardhan Kumar 22 9876543421 Delhi


2 Himani Sharma 32 8777384798 Mumbai
3 Aayush Sharma 24 8787878787 Kolkata
4 Hemsmith Sharma 25 1314516167 Bengaluru
5 Sewathi Kapoor 26 7676767676 Hyderabad

PROJECTID EMPLOYEEID CLIENTID PROJECTNAME


111 4 3 P1

222 2 1 P2

333 3 5 P3

444 3 2 P4
Q1. Perform Inner join on the given tables.
select * from employee INNER JOIN projects ON employee.EmpID =
projects.EmpID;

EMPID EMPFNAME EMPLNAME AGE PHONENO ADDRESS PROJECTID EMPID CLIENTID PROJECTNAME
2 Himani Sharma 32 8777384798 Mumbai 222 2 1 P2
3 Aayush Sharma 24 8787878787 Kolkata 333 3 5 P3
3 Aayush Sharma 24 8787878787 Kolkata 444 3 2 P4
4 Hemsmith Sharma 25 1314516167 Bengaluru 111 4 3 P1

Q2. Perform Left join on the given tables.


select * from employee INNER JOIN projects ON employee.Employee_ID =
projects.Employee_ID;

EMPID EMPFNAME EMPLNAME AGE PHONENO ADDRESS PROJECTID EMPID CLIENTID PROJECTNAME
4 Hemsmith Sharma 25 1314516167 Bengaluru 111 4 3 P1

2 Himani Sharma 32 8777384798 Mumbai 222 2 1 P2

3 Aayush Sharma 24 8787878787 Kolkata 333 3 5 P3

3 Aayush Sharma 24 8787878787 Kolkata 444 3 2 P4

1 Vardhan Kumar 22 9876543421 Delhi - - - -

5 Sewathi Kapoor 26 7676767676 Hyderabad - - - -

Q3. Perform Right join on the given tables.


select * from employee INNER JOIN projects ON employee.Employee_ID =
projects.Employee_ID;

EMPID EMPFNAME EMPLNAME AGE PHONENO ADDRESS PROJECTID EMPID CLIENTID PROJECTNAME
2 Himani Sharma 32 8777384798 Mumbai 222 2 1 P2

3 Aayush Sharma 24 8787878787 Kolkata 333 3 5 P3

3 Aayush Sharma 24 8787878787 Kolkata 444 3 2 P4

4 Hemsmith Sharma 25 1314516167 Bengaluru 111 4 3 P1

Q4. Perform Fulljoin on the given tables.


select * from employee INNER JOIN projects ON employee.Employee_ID =
projects.Employee_ID;

EMPID EMPFNAME EMPLNAME AGE PHONENO ADDRESS PROJECTID EMPID CLIENTID PROJECTNAME
1 Vardhan Kumar 22 9876543421 Delhi - - - -

2 Himani Sharma 32 8777384798 Mumbai 222 2 1 P2

3 Aayush Sharma 24 8787878787 Kolkata 333 3 5 P3

3 Aayush Sharma 24 8787878787 Kolkata 444 3 2 P4

4 Hemsmith Sharma 25 1314516167 Bengaluru 111 4 3 P1

5 Sewathi Kapoor 26 7676767676 Hyderabad - - - -


PRACTICAL - 8
Aim: To study and apply the constraints on the table.
Constraints: These are the set of rules or conditions that are applied on the table to ensure the
overall validity, integrity and consistency of the data present in the table.
Types of Integrity Constraints:
1. NOT NULL: The NOT NULL constraint enforces a column to not accept null values.
2. UNIQUE: The UNIQUE constraint ensures that all values in a column are different.
3. PRIMARY KEY: The PRIMARY KEY constraint uniquely identifies each record in
a table. Primary keys must contain UNIQUE values, and cannot contain NULL values.
4. CHECK: The CHECK constraint is used to limit the value range that can be placed in
a column.
5. FOREIGN KEY: The FOREIGN KEY constraint is used to prevent actions that
would destroy links between tables.
Input:
create table person(
person_id char(20) PRIMARY KEY,
Fname char(20) NOT NULL,
Lname char(20) UNIQUE,
Age int CHECK (Age>=31));
insert into person values('1','Tarak','Mehta',31); --No Constraints
insert into person values('','Tarak','Meht',31); --Primary Key Constraint
insert into person values('3','','Meht@',31); --Not Null Constraint
insert into person values('4','Tarak','Mehta',31); --Unique Constraint
insert into person values('5','Tarak','Mehtaa',30); --Check Constraint
select * from person;
create table person2(
person_id char(20),
Fname char(20),
FOREIGN KEY (person_id) REFERENCES person(person_id));
insert into person2 values('7','Tarak'); --Foreign Key Constraint
Output:

PERSON_ID FNAME LNAME AGE

1 Tarak Mehta 31

ORA-01400: cannot insert NULL into ("SQL_JXJJDXUZMRIQVDWUQRAUPGYGB"."PERSON"."PERSON_ID") ORA-06512: at "SYS.DBMS_SQL", line 1721

ORA-01400: cannot insert NULL into ("SQL_JXJJDXUZMRIQVDWUQRAUPGYGB"."PERSON"."FNAME") ORA-06512: at "SYS.DBMS_SQL", line 1721

ORA-00001: unique constraint (SQL_JXJJDXUZMRIQVDWUQRAUPGYGB.SYS_C0088671811) violated ORA-06512: at "SYS.DBMS_SQL", line 1721

ORA-02290: check constraint (SQL_JXJJDXUZMRIQVDWUQRAUPGYGB.SYS_C0088671810) violated ORA-06512: at "SYS.DBMS_SQL", line 1721

ORA-02291: integrity constraint (SQL_JXJJDXUZMRIQVDWUQRAUPGYGB.SYS_C0088671813) violated - parent key not found ORA-06512: at
"SYS.DBMS_SQL", line 1721
PRACTICAL - 9
Aim: To perform the basic SQL commands Update, Alter, Between, Delete, Drop and
Truncate.
Theory:
Update: The update command is used to modify the existing record in a table.
Syntax:
UPDATE table_name
SET column1 = value1,
column 2 = value2,
where condition;
Alter: The alter command is used to add, delete or modify the columns in an existing table.
Syntax:
ALTER table table_name
ADD column_name datatype(size);
ALTER table table_name
DELETE column_name;
Between: The SQL between operator selects a value within a range. The between operator is
inclusive i.e begin and end values are included.
Syntax:
Select column_name
from table_name
column_name BETWEEN value1 and value2;
Delete: The delete statement is used to delete the existing records in a table.
Syntax:
DELETE from table_name;
DELETE from table_name
where condition;
Drop: The drop command is used to remove an existing table from the database.
Syntax:
DROP table table_name;
Truncate: The truncate command is used to delete the complete data from an existing table.
Syntax:
TRUNCATE table table_name;

Create table Employee with ID, Contact_Name, Age, City and Salary and perform
queries.
Input:
create table Employee (
ID_ int unique,
Contact_Name char(20),
Age int,
City char(20),
Salary int);
insert into Employee values(1, 'Ramesh', 32, 'Ahmedabad', 2000);
insert into Employee values(2, 'Khilan', 25, 'Delhi', 3000);
insert into Employee values(3, 'Kaushik', 23, 'Banglore', 4000);
insert into Employee values(4, 'Chitali', 25, 'Chennai', 5000);
insert into Employee values(5, 'Hardik', 27, 'Gurugram', 2000);
insert into Employee values(6, 'Komal', 22, 'Indore', 15000);
insert into Employee values(7, 'Muffy', 24, 'Bhopal', 9000);
select * from Employee;
Output:

ID_ CONTACT_NAME AGE CITY SALARY

1 Ramesh 32 Ahmedabad 2000


2 Khilan 25 Delhi 3000
3 Kaushik 23 Banglore 4000
4 Chitali 25 Chennai 5000
5 Hardik 27 Gurugram 2000
6 Komal 22 Indore 15000
7 Muffy 24 Bhopal 9000

Q1. Update the table Employee where ID_ = 1 with a new contact name which is
‘Ramesh Gupta’ and a new city which is ‘Delhi’.
UPDATE Employee SET Contact_Name = 'Ramesh Gupta', City = 'Delhi' where ID_ = 1;

ID_ CONTACT_NAME AGE CITY SALARY

1 Ramesh Gupta 32 Delhi 2000

Q2. Update the age of an Employee as 29 where salary = 4000.


UPDATE Employee SET Age = 29 where Salary = 4000;

ID_ CONTACT_NAME AGE CITY SALARY

3 Kaushik 29 Banglore 4000

Q3. Alter table Employee by adding a new column Phone_No.


ALTER table Employee ADD Phone_No int;

ID_ CONTACT_NAME AGE CITY SALARY PHONE_NO

1 Ramesh Gupta 32 Delhi 2000 -


2 Khilan 25 Delhi 3000 -
3 Kaushik 29 Banglore 4000 -
4 Chitali 25 Chennai 5000 -
5 Hardik 27 Gurugram 2000 -
6 Komal 22 Indore 15000 -
7 Muffy 24 Bhopal 9000 -

Q4. Add the values into the new column Phone_No w.r.t to each employee.
UPDATE Employee SET Phone_No = 9838483219 where ID_ = 1;
UPDATE Employee SET Phone_No = 3456789782 where ID_ = 2;
UPDATE Employee SET Phone_No = 2345312567 where ID_ = 3;
UPDATE Employee SET Phone_No = 9542323113 where ID_ = 4;
UPDATE Employee SET Phone_No = 3434653434 where ID_ = 5;
UPDATE Employee SET Phone_No = 6564342714 where ID_ = 6;
UPDATE Employee SET Phone_No = 6545453423 where ID_ = 7;

ID_ CONTACT_NAME AGE CITY SALARY PHONE_NO

1 Ramesh Gupta 32 Delhi 2000 9838483219


2 Khilan 25 Delhi 3000 3456789782
3 Kaushik 29 Banglore 4000 2345312567
4 Chitali 25 Chennai 5000 9542323113
5 Hardik 27 Gurugram 2000 3434653434
6 Komal 22 Indore 15000 6564342714
7 Muffy 24 Bhopal 9000 6545453423

Create a table Department with Emp_ID, Employee_Name, Department and


Contact_No and perform queries.
Input:
create table Department (
Emp_ID_ int unique,
Employee_Name char(20),
Department char(20),
Contact_No int);
insert into Department values(101,'Isha','E101',5634578686);
insert into Department values(102,'Priya','E104',9875235254);
insert into Department values(103,'Neha','E105',9384752163);
insert into Department values(104,'Rahul','E102',9876543218);
insert into Department values(105,'Abhishek','E101',7564343213);
Select * from Department;
Output:

EMP_ID_ EMPLOYEE_NAME DEPARTMENT CONTACT_NO

101 Isha E101 5634578686


102 Priya E104 9875235254
103 Neha E105 9384752163
104 Rahul E102 9876543218
105 Abhishek E101 7564343213

Q1. Select the details of employee who work either for department E104 or E102.
select * from Department WHERE Department = 'E104' OR Department = 'E102';

EMP_ID_ EMPLOYEE_NAME DEPARTMENT CONTACT_NO

102 Priya E104 9875235254

104 Rahul E102 9876543218

Q2. Update the employee name as Isha Yadav whose Emp_ID is 101.
UPDATE Department SET Employee_Name = 'Isha Yadav' where Emp_ID_ = 101;
EMP_ID_ EMPLOYEE_NAME DEPARTMENT CONTACT_NO

101 Isha Yadav E101 5634578686

Q3. Delete the record from the existing table where Employee_Name is Abhishek.
DELETE from Department where Employee_Name = 'Abhishek';

EMP_ID_ EMPLOYEE_NAME DEPARTMENT CONTACT_NO

101 Isha Yadav E101 5634578686


102 Priya E104 9875235254
103 Neha E105 9384752163
104 Rahul E102 9876543218

Q4. Alter the given table by adding Department_Name also write the values w.r.t the
newly added column.
ALTER table Department ADD Department_Name char(20);
UPDATE Department SET Department_Name = 'Management' where Department =
'E101';
UPDATE Department SET Department_Name = 'Development' where Department =
'E102';
UPDATE Department SET Department_Name = 'Quality Assurance' where Department =
'E103';
UPDATE Department SET Department_Name = 'Sales' where Department = 'E104';
UPDATE Department SET Department_Name = 'Marketing' where Department = 'E105';

EMP_ID_ EMPLOYEE_NAME DEPARTMENT CONTACT_NO DEPARTMENT_NAME

101 Isha Yadav E101 5634578686 Management


102 Priya E104 9875235254 Sales
103 Neha E105 9384752163 Marketing
104 Rahul E102 9876543218 Development
PRACTICAL - 10
Aim: To perform SQL LIKE operator on relational database.
Theory:
LIKE operator is used in WHERE clause to search dor a specific pattern in a column.
% Symbol: 0,1 or multiple characters.
_ Symbol: Represents only a single character.
Syntax:
Select column1,column2
From tablename
WHERE column1 LIKE pattern;
Input:
create table student(
StudentID int,
StudentName char(20));
insert into student values(1,'akash');
insert into student values(2,'mitali');
insert into student values(3,'sanjay');
insert into student values(4,'anuj');
insert into student values(5,'sonali');
select * from student;
create table customer(
CustomerID int,
FirstName char(20),
LastName char(20));
insert into customer values(1,'jackson','joe');
insert into customer values(2,'smith','jane');
insert into customer values(3,'reynolds','allen');
insert into customer values(4,'anderson','paige');
insert into customer values(5,'johnson','derek');
select * from customer;
Output:
Student
STUDENTID STUDENTNAME

1 akash

2 mitali

3 sanjay

4 anuj

5 sonali

Employee
CUSTOMERID FIRSTNAME LASTNAME

1 jackson joe

2 smith jane
3 reynolds allen

4 anderson paige

5 johnson derek

Q1. Retrieve data from student where student name starts with ‘a’.
select * from student where StudentName LIKE 'a%';

STUDENTID STUDENTNAME

1 akash

4 anuj

Q2. Retrieve data from student where student name ends with ‘i’.
select * from student where StudentName LIKE '%i';

STUDENTID STUDENTNAME

2 mitali

5 sonali

Q3. Retrieve data from student where student name has ‘an’ at any position.
select * from student where StudentName LIKE '%an%';

STUDENTID STUDENTNAME

3 sanjay

4 anuj

Q4. Retrieve data from student where student name has ‘o’ at 2nd position.
select * from student where StudentName LIKE '_o%';

STUDENTID STUDENTNAME

5 sonali

Q5. Retrieve data from student whose 3rd alphabet is ‘n’.


select * from student where StudentName LIKE '__n%';

STUDENTID STUDENTNAME

3 sanjay

5 sonali

Q6. Retrieve data from student whose name starts with ‘a’ and has atleast 2
characters.
select * from student where StudentName LIKE 'a__%';

STUDENTID STUDENTNAME

1 akash

4 anuj

Q7. Retrieve data from student where student name starts with ‘m’ and ends with ‘i’.
select * from student where StudentName LIKE 'm%i';

STUDENTID STUDENTNAME

2 mitali

Q1. Retrieve the last name that starts with ‘j’;


select * from customer where LastName LIKE 'j%';

CUSTOMERID FIRSTNAME LASTNAME

1 jackson joe

2 smith jane

Q2. Retrieve the first name that has ‘a’ in 2nd position’;
select * from customer where FirstName LIKE '_a%';

CUSTOMERID FIRSTNAME LASTNAME

1 jackson joe

Q3. Retrieve the last name that starts with ‘d’ and end with ‘k’;
select * from customer where LastName LIKE 'd%k';

CUSTOMERID FIRSTNAME LASTNAME

5 johnson derek

Q4. Retrieve the first name that has ‘ol’ at any position;
select * from customer where FirstName LIKE '%ol%';

CUSTOMERID FIRSTNAME LASTNAME

3 reynolds allen
PRACTICAL - 11
Aim: To perform the aggregation function in SQL.
Theory:
MIN(): The MIN() aggregate function returns the lowest value (minimum) in a set of
non-NULL values.
Syntax:
select MIN(cloumn name)
from tablename
where condition;
MAX(): The MAX() aggregate function returns the highest value (maximum) in a set of
non-NULL values.
Syntax:
select MAX(cloumn name)
from tablename
where condition;
COUNT(): The COUNT() function returns the number of rows in a database table.
Syntax:
select COUNT(cloumn name)
from tablename
where condition;
AVG(): The AVG() function calculates the average of a set of values.
Syntax:
select AVG(cloumn name)
from tablename
where condition;
SUM(): The SUM() function returns the total sum of a numeric column.
Syntax:
select SUM(cloumn name)
from tablename
where condition;
Input:
create table employee(
EID char(20),
EName char(20),
Age int,
City char(20),
Salary int);
insert into employee values('E001','Raj',23,'Ahemdabad',2000);
insert into employee values('E002','Mohit',23,'Delhi',3000);
insert into employee values('E003','Daniel',21,'Mumbai',4000);
insert into employee values('E004','Sania',24,'Indore',5000);
insert into employee values('E005','Tayal',25,'Udaipur',4000);
select * from employee;
Output:

EID ENAME AGE CITY SALARY


E001 Raj 23 Ahemdabad 2000
E002 Mohit 23 Delhi 3000
E003 Daniel 21 Mumbai 4000
E004 Sania 24 Indore 5000
E005 Tayal 25 Udaipur 4000

Q1. Apply aggregate function MIN on Salary.


select MIN(Salary) from employee;

MIN(SALARY)

2000

Q2. Apply aggregate function MAX on Salary.


select MAX(Salary) from employee;

MAX(SALARY)

5000

Q3. Apply aggregate function AVG on Age.


select AVG(Age) from employee;

AVG(AGE)

23.2

Q4. Apply aggregate function Sum on Salary.


select SUM(Salary) from employee;

SUM(SALARY)

18000

You might also like