Professional Documents
Culture Documents
DMBS11 Exp
DMBS11 Exp
Output:
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:
4 around the horn thomas hardy 120 hanover sq. london WA1 1DP UK
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
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
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
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
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:
6 Komal 22 MP 4500
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';
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
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));
10308 2 18-9-21
10309 37 21-10-21
10310 77 01-01-21
Customer1
CUSTOMERID CUSTOMERNAME CONTACTNAME COUNTRY
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;
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
EMPID EMPFNAME EMPLNAME AGE PHONENO ADDRESS PROJECTID EMPID CLIENTID PROJECTNAME
4 Hemsmith Sharma 25 1314516167 Bengaluru 111 4 3 P1
EMPID EMPFNAME EMPLNAME AGE PHONENO ADDRESS PROJECTID EMPID CLIENTID PROJECTNAME
2 Himani Sharma 32 8777384798 Mumbai 222 2 1 P2
EMPID EMPFNAME EMPLNAME AGE PHONENO ADDRESS PROJECTID EMPID CLIENTID PROJECTNAME
1 Vardhan Kumar 22 9876543421 Delhi - - - -
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-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:
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;
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;
Q1. Select the details of employee who work either for department E104 or E102.
select * from Department WHERE Department = 'E104' OR Department = 'E102';
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
Q3. Delete the record from the existing table where Employee_Name is Abhishek.
DELETE from Department where Employee_Name = 'Abhishek';
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';
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
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
1 jackson joe
2 smith jane
Q2. Retrieve the first name that has ‘a’ in 2nd position’;
select * from customer where FirstName LIKE '_a%';
1 jackson joe
Q3. Retrieve the last name that starts with ‘d’ and end with ‘k’;
select * from customer where LastName LIKE 'd%k';
5 johnson derek
Q4. Retrieve the first name that has ‘ol’ at any position;
select * from customer where FirstName LIKE '%ol%';
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:
MIN(SALARY)
2000
MAX(SALARY)
5000
AVG(AGE)
23.2
SUM(SALARY)
18000