0% found this document useful (0 votes)
64 views40 pages

SQL Final

Uploaded by

Ye Duniya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views40 pages

SQL Final

Uploaded by

Ye Duniya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Information Systems Management Lab

Subject Code : BBA-307

Submitted by: Submitted to :


Name: Abu Farhan Dr. Pawan Kumar
Course : BBA (2022-25) Asst. Prof. (IT)
Semester: 05th
University Enrolment No: 02018401722

1|Page
Table of Contents

Sr. Topic Page No. Faculty Signature


No
1 How SQL is used in a DBMS to retrieve the data 4
2 Discuss the characteristics of SQL in brief 4
3 Give the SQL expression for the following queries with
respect to above tables:
(a)
(a) Find name for all employees who work for the 9
“Accounts” department.
(b)
(b) For each employee, retrieve the employee’s name and 9
the name of his/her employee supervisor.
(c)(c) How many employees work in the “Accounts” 10
department
(d)
(d) What are maximum , minimum and average salary in 10
the “Accounts” department
(e)
(e) Retrieve the department names for each department in 11
which more than two employees work
(f)(f) Modify the database so that “Michael” is assigned to the 11
“Accounts” department
(g)
(g) Give all employees of “Sales” department a 10% rise 12
in salary
(h) Delete all rows for employees of “Apprentice”
(h) 13
department
(i)4 There is a table named "PEOPLE" that has four columns:
Country, Continent, Area, and Population. The first two
columns are character types and the last two columns are
numeric types. Now write the appropriate SQL queries for the
following:
(j) (k)
(a) List the countries that have at least 5,00,000 people. 16
[Show Country, Population]
(l) (m)
(b) Identify the countries, which have the word "United" 16
included in their names
(n) (o)
(c) List each country name where the population is larger 17
than "United States."
(p) (d) For each continent, show the number of countries with 17
populations of at least 10 million
(q) (r)
(e) List the continents with total populations of at 18
least 100 million
5 Create the following table with database AIMT.
(a) Select the details of the employee whose name starts 27
with P.
(b) How many permanent candidate take salary more than 27
5000.
(c) Select the detail of employee whose email ID is in 27
gmail.
(d) Select the details of the employee who work either for 28
department E – 104 or E – 102.
(e) What is the department name for DeptID E – 102 28

2|Page
(f) What is total salary that is paid to permanent 28
employees
(g) List name of all employees whose name ends with a. 29
(h) List the number of Department of employees in each 29
project.
(i) How many project started in year 2010. 29
(j) How many project started and finished in the same 30
year.
(k) Select the name of the employee whose name’s 3rd 30
character is ‘h’.
(l) Update the Rahul email id to [email protected]. 31
(m) Change the column name EndYear to EYear in 31
EmpProject Table.
(n) Use the foreign key concept on any two tables. 32
(o) Apply join concepts on above tables. 33-34
(p) Select the department name of the company which is 35
assigned to the employee whose employee id is greater
than 103.
(q) Select the name of the employee who is working 35
under Abhishek.
(r) Select the name of the employee who is department 36
head of HR.
(s) Select the name of the employee head who is 36
permanent.
(t) Select the name and email of the department head who 37
is not Permanent.
(u) Select the employee whose department off is 37
Monday.
(v) Select the Indian clients details. 38
(w) Select the details of all employees working in 38
development department.
(x) Use the aggregate functions. 38-40

3|Page
Q1.How SQL is used in a DBMS to retrieve the data ?

ANS: SQL (Structured Query Language) is the standard language used to communicate with a
database management system (DBMS) to retrieve, insert, update, and manage data. When retrieving
data, SQL uses the SELECT statement to query and fetch records from one or more tables within a
database. Here’s how it works:
Basic SELECT Statement
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Ex: SELECT name, department


FROM employees
WHERE department = 'IT';

Q2. Discuss the characteristics of SQL in brief ?

ANS: SQL (Structured Query Language) has several key characteristics that make it essential for
interacting with relational databases. Some of its primary characteristics:

1.Declarative Language
SQL is a declarative language, which means users specify what they want to retrieve or manipulate,
not how the data should be processed. This makes it easier to write and understand queries.

2. Data Definition Language (DDL)


SQL can define the structure of a database, including creating, altering, and deleting tables and
other database objects.
Commands: CREATE, ALTER, DROP, TRUNCATE

3. Data Manipulation Language (DML)


SQL can be used to manipulate and retrieve data stored in the database. Users can insert, update,
delete, and retrieve records.
Commands: SELECT, INSERT, UPDATE, DELETE

4. Data Control Language (DCL)


SQL provides commands to control access to the data within the database, including granting and
revoking permissions.
Commands: GRANT, REVOKE

5. Transaction Control Language (TCL)


SQL supports transactions, which are a sequence of operations performed as a single logical unit
of work. It ensures data integrity and consistency.
Commands: COMMIT, ROLLBACK, SAVEPOINT

6. Set-Based Operations
SQL is designed to work with sets of data. This means that operations can be performed on
multiple rows at once, rather than processing row by row. This makes SQL efficient for handling
bulk data.

7. Standardized Language
SQL is a standardized language (e.g., ANSI, ISO), meaning that it can be used across different
database systems (like MySQL, PostgreSQL, Oracle, and SQL Server), though there may be some
system-specific extensions.

4|Page
Q3. Consider the following tables:
Structure of EMPLOYEE Table
Field Name Data Type Description
Emp_ID Numeric Employee’s ID number
Dept_ID Numeric Department’s ID number
Emp_Name Character Employee’s name
Employee’s Salary Numeric Salary

Structure of DEPARTMENT Table


Dept_ID Data Type Description
Dept_ID Numeric Department’s ID number
Dept_Name Character Department’s name
Supervisor Character Department supervisor’s name

Give the SQL expression for the following queries with respect to above tables.
(s) Find name for all employees who work for the “Accounts” department.
(t) For each employee, retrieve the employee’s name and the name of his/her employee supervisor.
(u) How many employees work in the “Accounts” department.
(v) What are maximum , minimum and average salary in the “Accounts” department.
(w) Retrieve the department names for each department in which more than two employees work.
(x) Modify the database so that “Michael” is assigned to the “Accounts” department.
(y) Give all employees of “Sales” department a 10% rise in salary.
(z) Delete all rows for employees of “Apprentice” department.
ANS: Tables:
CREATE TABLE DEPARTMENT (
Dept_ID INT PRIMARY KEY,
Dept_Name VARCHAR(100),
Supervisor VARCHAR(100));
DESC DEPARTMENT;

Output:

INSERT INTO DEPARTMENT (Dept_ID, Dept_Name, Supervisor)


VALUES
(101, 'Accounts', 'Farhan');
INSERT INTO DEPARTMENT (Dept_ID, Dept_Name, Supervisor)
VALUES
(102, 'Sales', 'Nikita');
INSERT INTO DEPARTMENT (Dept_ID, Dept_Name, Supervisor)

5|Page
VALUES
(103, 'Apprentice', 'Johhny');
INSERT INTO DEPARTMENT (Dept_ID, Dept_Name, Supervisor)
VALUES
(104, 'HR', 'Punita');
INSERT INTO DEPARTMENT (Dept_ID, Dept_Name, Supervisor)
VALUES
(105, 'Marketing', 'Punni');
select * from DEPARTMENT

CREATE TABLE EMPLOYEE (


Emp_ID INT,
Dept_ID INT,
Emp_Name VARCHAR(50),
Employee_Salary DECIMAL(10, 2),
CONSTRAINT FK_DEPARTEMP FOREIGN KEY (Dept_ID) REFERENCES
DEPARTMENT(Dept_ID));
desc EMPLOYEE;

Output:

INSERT INTO EMPLOYEE (Emp_ID, Dept_ID, Emp_Name, Employee_Salary)


VALUES
(1, 101, 'Abhishek', 50000);
INSERT INTO EMPLOYEE (Emp_ID, Dept_ID, Emp_Name, Employee_Salary)
VALUES
(2, 101, 'Sanjeev', 60000);
INSERT INTO EMPLOYEE (Emp_ID, Dept_ID, Emp_Name, Employee_Salary)
VALUES
(3, 101, 'Shivani', 45000);
INSERT INTO EMPLOYEE (Emp_ID, Dept_ID, Emp_Name, Employee_Salary)

6|Page
VALUES
(4, 101, 'Geeta', 55000);
INSERT INTO EMPLOYEE (Emp_ID, Dept_ID, Emp_Name, Employee_Salary)
VALUES
(5, 101, 'Mohit', 70000);
INSERT INTO EMPLOYEE (Emp_ID, Dept_ID, Emp_Name, Employee_Salary)
VALUES
(6, 102, 'Dwij', 73000);
INSERT INTO EMPLOYEE (Emp_ID, Dept_ID, Emp_Name, Employee_Salary)
VALUES
(7, 102, 'Taniska', 63000);
INSERT INTO EMPLOYEE (Emp_ID, Dept_ID, Emp_Name, Employee_Salary)
VALUES
(8, 102, 'Dewanshi', 81000);
INSERT INTO EMPLOYEE (Emp_ID, Dept_ID, Emp_Name, Employee_Salary)
VALUES
(9, 103, 'Sakshi', 46000);
INSERT INTO EMPLOYEE (Emp_ID, Dept_ID, Emp_Name, Employee_Salary)
VALUES
(10, 103, 'Vishal', 69000);
INSERT INTO EMPLOYEE (Emp_ID, Dept_ID, Emp_Name, Employee_Salary)
VALUES
(11, 103, 'Abhitesh', 77000);
INSERT INTO EMPLOYEE (Emp_ID, Dept_ID, Emp_Name, Employee_Salary)
VALUES
(12, 104, 'Tanmoy', 75000);
INSERT INTO EMPLOYEE (Emp_ID, Dept_ID, Emp_Name, Employee_Salary)
VALUES
(13, 104, 'Alisha', 34000);
INSERT INTO EMPLOYEE (Emp_ID, Dept_ID, Emp_Name, Employee_Salary)
VALUES
(14, 105, 'Jatin', 89000);
INSERT INTO EMPLOYEE (Emp_ID, Dept_ID, Emp_Name, Employee_Salary)
VALUES
(15, 105, 'Michael', 57000);
SELECT * FROM EMPLOYEE

7|Page
8|Page
Questions:

(a) Find name for all employees who work for the “Accounts” department.
Ans: SELECT Emp_Name FROM EMPLOYEE
JOIN DEPARTMENT ON EMPLOYEE.Dept_ID = DEPARTMENT.Dept_ID
WHERE DEPARTMENT.Dept_Name = 'Accounts';

Output:

(b) For each employee, retrieve the employee’s name and the name of his/her employee
supervisor.
Ans: SELECT Emp_Name, Supervisor FROM EMPLOYEE
JOIN DEPARTMENT ON EMPLOYEE.Dept_ID = DEPARTMENT.Dept_ID;

Output:

9|Page
c) How many employees work in the “Accounts” department.
Ans: SELECT COUNT(*) AS Employee_Count FROM EMPLOYEE
JOIN DEPARTMENT ON EMPLOYEE.Dept_ID = DEPARTMENT.Dept_ID
WHERE DEPARTMENT.Dept_Name = 'Accounts';

Output:

d) What are maximum , minimum and average salary in the “Accounts” department.
Ans: SELECT
MAX(Employee_Salary) AS Max_Salary,
MIN(Employee_Salary) AS Min_Salary,
AVG(Employee_Salary) AS Avg_Salary
FROM EMPLOYEE JOIN DEPARTMENT ON EMPLOYEE.Dept_ID =
DEPARTMENT.Dept_ID
WHERE DEPARTMENT.Dept_Name = 'Accounts';

Output:

e) Retrieve the department names for each department in which more than two employees
work.
Ans: SELECT Dept_Name
FROM EMPLOYEE
JOIN DEPARTMENT ON EMPLOYEE.Dept_ID = DEPARTMENT.Dept_ID
GROUP BY Dept_Name
HAVING COUNT(EMPLOYEE.Emp_ID) > 2;

Output:

10 | P a g e
(f) Modify the database so that “Michael” is assigned to the “Accounts” department.
Ans: UPDATE EMPLOYEE
SET Dept_ID = (SELECT Dept_ID FROM DEPARTMENT WHERE Dept_Name =
'Accounts')
WHERE Emp_Name = 'Michael';
SELECT * FROM EMPLOYEE;

Output:

11 | P a g e
(g) Give all employees of “Sales” department a 10% rise in salary.
Ans: UPDATE EMPLOYEE
SET Employee_Salary = Employee_Salary * 1.10
WHERE Dept_ID = (SELECT Dept_ID FROM DEPARTMENT WHERE Dept_Name =
'Sales');
SELECT * FROM EMPLOYEE;

Output:

12 | P a g e
(h) Delete all rows for employees of “Apprentice” department.
Ans: DELETE FROM EMPLOYEE
WHERE Dept_ID = (SELECT Dept_ID FROM DEPARTMENT WHERE Dept_Name =
'Apprentice');

Output:

Q4. There is a table named "PEOPLE" that has four columns: Country, Continent, Area, and
Population. The first two columns are character types and the last two columns are numeric
types. Now write the appropriate SQL queries for the following:

(a) List the countries that have at least 5,00,000 people. [Show Country, Population]
(b) Identify the countries, which have the word "United" included in their names.
(c) List each country name where the population is larger than "United States."
(d) For each continent, show the number of countries with populations of at least 10 million.
(e) List the continents with total populations of at least 100 million.

Ans: Table:
CREATE TABLE PEOPLE (
Country varchar(100),
Continent varchar(100),
Area INT,
Population INT);
DESC PEOPLE;

13 | P a g e
Output:

INSERT INTO PEOPLE (Country, Continent, Area, Population)


VALUES
('United States', 'North America', 9833517, 331002651);
INSERT INTO PEOPLE (Country, Continent, Area, Population)
VALUES
('Canada', 'North America', 9984670, 37742154);
INSERT INTO PEOPLE (Country, Continent, Area, Population)
VALUES
('Brazil', 'South America', 8515767, 212559417);
INSERT INTO PEOPLE (Country, Continent, Area, Population)
VALUES
('United Kingdom', 'Europe', 243610, 67886011);
INSERT INTO PEOPLE (Country, Continent, Area, Population)
VALUES
('Germany', 'Europe', 357022, 83783942);
INSERT INTO PEOPLE (Country, Continent, Area, Population)
VALUES
('India', 'Asia', 3287263, 1380004385);
INSERT INTO PEOPLE (Country, Continent, Area, Population)
VALUES
('China', 'Asia', 9596961, 1439323776);
INSERT INTO PEOPLE (Country, Continent, Area, Population)
VALUES
('Australia', 'Oceania', 7692024, 25499884);
INSERT INTO PEOPLE (Country, Continent, Area, Population)
VALUES
('Russia', 'Europe', 17098242, 145934462);
INSERT INTO PEOPLE (Country, Continent, Area, Population)
VALUES
('Nigeria', 'Africa', 923768, 206139589);
INSERT INTO PEOPLE (Country, Continent, Area, Population)
VALUES
('Egypt', 'Africa', 1010408, 102334404);
INSERT INTO PEOPLE (Country, Continent, Area, Population)
VALUES
('United Arab Emirates', 'Asia', 83600, 9890402);
SELECT * from PEOPLE

14 | P a g e
15 | P a g e
Questions:

(a) List the countries that have at least 5,00,000 people. [Show Country, Population]
Ans: SELECT Country, Population
from PEOPLE
where Population>=500000;

Output:

(b) Identify the countries, which have the word "United" included in their names.
Ans: SELECT Country from PEOPLE
where Country like '%United%';

Output:

16 | P a g e
(c) List each country name where the population is larger than "United States."
Ans: SELECT Country, Population
from PEOPLE
where Population >(SELECT Population from PEOPLE where Country='United States');

Output:

(d) For each continent, show the number of countries with populations of at least 10 million.
Ans: SELECT Continent, COUNT(Country) AS NumberOfCountries
FROM PEOPLE
WHERE Population >= 10000000
GROUP BY Continent;

Output:

(e) List the continents with total populations of at least 100 million.
Ans: SELECT Continent
FROM PEOPLE
GROUP BY Continent
HAVING SUM(Population) >= 100000000;

Output:

17 | P a g e
Q5. Create the following table with database AIMT.
Table Name :- Employee

EmpId EmpName Department ContactNo EmailId EmpHeadId


101 Isha E – 101 1234567890 [email protected] 105
102 Priya E - 102 1234567890 [email protected] 103
103 Neha E - 101 1234567890 [email protected] 101
104 Rahul E - 102 1234567890 [email protected] 105
105 Abhishek E - 101 1234567890 [email protected] 102

Table :- EmpDept
DeptId DeptName Dept_off DeptHead
E – 101 HR Monday 105
E - 102 Development Tuesday 101
E - 103 House Keeping Saturday 103
E - 104 Sales Sunday 104
E - 105 Purchase Tuesday 104

Table :- EmpSalary

EmpId Salary IsPermanent


101 2000 Yes
102 10000 Yes
103 5000 No
104 1900 Yes
105 2300 Yes

Table :- Project
ProjectId Duration
p-1 23
p-2 15
p-3 45
p-4 2
p-5 30

Table :- Country
cid cname
c-1 India
c-2 USA
c-3 China
c-4 Pakistan
c-5 Russia

Table :- ClientTable

18 | P a g e
ClientId ClientName cid
cl-1 ABC Group c-1
cl-2 PQR c-1
cl-3 XYZ c-2
cl-4 tech altum c-3
cl-5 mnp c-5

Table :- EmpProject

EmpId ProjectId ClientId StartYear EndYear


101 p-1 cl-1 2010 2010
102 p-2 cl-2 2010 2012
103 p-1 cl-3 2013
104 p-4 cl-1 2014 2015
105 p-4 cl-5 2015

1) Select the details of the employee whose name starts with P.


2) How many permanent candidate take salary more than 5000.
3) Select the detail of employee whose email ID is in gmail.
4) Select the details of the employee who work either for department E – 104 or E – 102.
5) What is the department name for DeptID E – 102 ?
6) What is total salary that is paid to permanent employees ?
7) List name of all employees whose name ends with a.
8) List the number of Department of employees in each project.
9) How many project started in year 2010.
10) How many project started and finished in the same year.
11) Select the name of the employee whose name’s 3rd character is ‘h’.
12) Update the Rahul email id to [email protected].
13) Change the column name EndYear to EYear in EmpProject Table.
14) Use the foreign key concept on any two tables.
15) Apply join concepts on above tables.
16) Select the department name of the company which is assigned to the employee whose
employee id is greater than 103.
17) Select the name of the employee who is working under Abhishek.
18) Select the name of the employee who is department head of HR.
19) Select the name of the employee head who is permanent.
20) Select the name and email of the department head who is not Permanent.
21) Select the employee whose department off is Monday.
22) Select the Indian clients details.
23) Select the details of all employees working in development department.
24) Use the aggregate functions.

19 | P a g e
ANS: Tables:
CREATE TABLE Abu_Employee (
EmpId INT PRIMARY KEY,
EmpName VARCHAR(50),
Department VARCHAR(20),
ContactNo VARCHAR(10),
EmailId VARCHAR(50),
EmpHeadId INT);
desc Abu_Employee;

Output:

INSERT INTO Abu_Employee (EmpId, EmpName, Department, ContactNo, EmailId, EmpHeadId)


VALUES
(101, 'Isha', 'E-101', '1234567890', '[email protected]', 105);
INSERT INTO Abu_Employee (EmpId, EmpName, Department, ContactNo, EmailId, EmpHeadId)
VALUES
(102, 'Priya', 'E-104', '1234567890', '[email protected]', 103);
INSERT INTO Abu_Employee (EmpId, EmpName, Department, ContactNo, EmailId, EmpHeadId)
VALUES
(103, 'Neha', 'E-101', '1234567890', '[email protected]', 101);
INSERT INTO Abu_Employee (EmpId, EmpName, Department, ContactNo, EmailId, EmpHeadId)
VALUES
(104, 'Rahul', 'E-102', '1234567890', '[email protected]', 105);
INSERT INTO Abu_Employee (EmpId, EmpName, Department, ContactNo, EmailId, EmpHeadId)
VALUES
(105, 'Abhishek', 'E-101', '1234567890', '[email protected]', 102);
select * from Abu_Employee

20 | P a g e
CREATE TABLE EmpDept (
DeptId VARCHAR(10) PRIMARY KEY,
DeptName VARCHAR(50),
DeptOff VARCHAR(20),
DeptHead INT);
desc EmpDept

Output:

INSERT INTO EmpDept (DeptId, DeptName, DeptOff, DeptHead)


VALUES
('E-101', 'HR', 'Monday', 105);
INSERT INTO EmpDept (DeptId, DeptName, DeptOff, DeptHead)
VALUES
('E-102', 'Development', 'Tuesday', 101);
INSERT INTO EmpDept (DeptId, DeptName, DeptOff, DeptHead)
VALUES
('E-103', 'House Keeping', 'Saturday', 103);
INSERT INTO EmpDept (DeptId, DeptName, DeptOff, DeptHead)
VALUES
('E-104', 'Sales', 'Sunday', 104);
INSERT INTO EmpDept (DeptId, DeptName, DeptOff, DeptHead)
VALUES
('E-105', 'Purchase', 'Friday', 102);
Select * from EmpDept;

21 | P a g e
CREATE TABLE EmpSalary (
EmpId INT PRIMARY KEY,
Salary INT,
IsPermanent VARCHAR(3),
FOREIGN KEY (EmpId) REFERENCES Abu_Employee(EmpId));
Desc EmpSalary;

Output:

INSERT INTO EmpSalary (EmpId, Salary, IsPermanent)


VALUES
(101, 2000, 'Yes');
INSERT INTO EmpSalary (EmpId, Salary, IsPermanent)
VALUES
(102, 10000, 'Yes');
INSERT INTO EmpSalary (EmpId, Salary, IsPermanent)
VALUES
(103, 5000, 'No');
INSERT INTO EmpSalary (EmpId, Salary, IsPermanent)
VALUES
(104, 1900, 'Yes');
INSERT INTO EmpSalary (EmpId, Salary, IsPermanent)
VALUES
(105, 2300, 'Yes');
select * from EmpSalary

22 | P a g e
CREATE TABLE Project (
ProjectId VARCHAR(5) PRIMARY KEY,
Duration INT);
desc Project

Output:

INSERT INTO Project (ProjectId, Duration)


VALUES
('p-1', 23);
INSERT INTO Project (ProjectId, Duration)
VALUES
('p-2', 15);
INSERT INTO Project (ProjectId, Duration)
VALUES
('p-3', 45);
INSERT INTO Project (ProjectId, Duration)
VALUES
('p-4', 2);
INSERT INTO Project (ProjectId, Duration)
VALUES
('p-5', 30);
Select * from Project

23 | P a g e
CREATE TABLE Country (
cid VARCHAR(5) PRIMARY KEY,
cname VARCHAR(20));
Desc Country;

Output:

INSERT INTO Country (cid, cname)


VALUES
('c-1', 'India');
INSERT INTO Country (cid, cname)
VALUES
('c-2', 'USA');
INSERT INTO Country (cid, cname)
VALUES
('c-3', 'China');
INSERT INTO Country (cid, cname)
VALUES
('c-4', 'Pakistan');
INSERT INTO Country (cid, cname)
VALUES
('c-5', 'Russia');
select * from Country

24 | P a g e
CREATE TABLE ClientTable (
ClientId VARCHAR(5) PRIMARY KEY,
ClientName VARCHAR(20),
cid VARCHAR(5));
Desc ClientTable;

Output:

INSERT INTO ClientTable (ClientId, ClientName, cid)


VALUES
('cl-1', 'ABC Group', 'c-1');
INSERT INTO ClientTable (ClientId, ClientName, cid)
VALUES
('cl-2', 'POR', 'c-1');
INSERT INTO ClientTable (ClientId, ClientName, cid)
VALUES
('cl-3', 'XYZ', 'c-2');
INSERT INTO ClientTable (ClientId, ClientName, cid)
VALUES
('cl-4', 'tech altum', 'c-3');
INSERT INTO ClientTable (ClientId, ClientName, cid)
VALUES
('cl-5', 'mnp', 'c-5');
Select * from ClientTable

25 | P a g e
CREATE TABLE EmpProject (
EmpId INT,
ProjectId VARCHAR(5),
ClientId VARCHAR(5),
StartYear INT,
EndYear INT,
FOREIGN KEY (EmpId) REFERENCES Abu_Employee(EmpId),
FOREIGN KEY (ProjectId) REFERENCES Project(ProjectId),
FOREIGN KEY (ClientId) REFERENCES ClientTable(ClientId));
desc EmpProject;

Output:

INSERT INTO EmpProject (EmpId, ProjectId, ClientId, StartYear, EndYear)


VALUES
(101, 'p-1', 'cl-1', 2010, 2010);
INSERT INTO EmpProject (EmpId, ProjectId, ClientId, StartYear, EndYear)
VALUES
(102, 'p-2', 'cl-2', 2010, 2012);
INSERT INTO EmpProject (EmpId, ProjectId, ClientId, StartYear, EndYear)
VALUES
(103, 'p-1', 'cl-3', 2013, NULL);
INSERT INTO EmpProject (EmpId, ProjectId, ClientId, StartYear, EndYear)
VALUES
(104, 'p-4', 'cl-1', 2014, 2015)
INSERT INTO EmpProject (EmpId, ProjectId, ClientId, StartYear, EndYear)
VALUES
(105, 'p-4', 'cl-5', 2015, NULL);
select * from EmpProject

26 | P a g e
Questions:
1) Select the details of the employee whose name starts with P.
Ans: Select EmpName from Abu_Employee
where EmpName like '%P%';
Output:

2) How many permanent candidate take salary more than 5000.


Ans: SELECT COUNT(*)
FROM EmpSalary
WHERE Salary > 5000 and IsPermanent = 'Yes';
Output:

3) Select the detail of employee whose email ID is in gmail.


Ans: SELECT * FROM Abu_Employee
WHERE EmailId LIKE '%@gmail.com';

Output:

27 | P a g e
4) Select the details of the employee who work either for department E – 104 or E – 102.
Ans: SELECT *
FROM Abu_Employee
WHERE Department IN ('E-104', 'E-102');

Output:

5) What is the department name for DeptID E – 102 ?


Ans: SELECT DeptName
FROM EmpDept
WHERE DeptID = 'E-102';

Output:

6) What is total salary that is paid to permanent employees ?


Ans: SELECT SUM(Salary) AS TotalSalary
FROM EmpSalary
WHERE IsPermanent = 'Yes';

Output:

28 | P a g e
7) List name of all employees whose name ends with a ?
Ans: SELECT *
FROM Abu_Employee
WHERE EmpName LIKE'%a';
Output:

8) List the number of Department of employees in each project ?


Ans: select COUNT(EmpId) as Employee, ProjectId
from EmpProject
GROUP BY ProjectId;

Output:

9) How many project started in year 2010 ?


Ans: select count(*) as Project_Count
from EmpProject
where (StartYear) = 2010;

Output:

29 | P a g e
10) How many project started and finished in the same year ?
Ans: select count(*) as Project_Count
from EmpProject
where (StartYear) = (EndYear);

Output:

11) Select the name of the employee whose name’s 3rd character is ‘h’ ?
Ans: select EmpName
from Abu_Employee
where EmpName LIKE '__h%';

Output:

30 | P a g e
12) Update the Rahul email id to [email protected] ?
Ans: UPDATE Abu_Employee
SET EmailId = '[email protected]'
where EmpName = 'Rahul';
SELECT * FROM Abu_Employee;
Output:

13) Change the column name EndYear to EYear in EmpProject Table ?


Ans: Alter table EmpProject
RENAME COLUMN EndYear TO EYear;
SELECT * FROM EmpProject;

Output:

31 | P a g e
14) Use the foreign key concept on any two tables ?
Ans: CREATE TABLE EmpSalary (
EmpId INT PRIMARY KEY,
Salary INT,
IsPermanent VARCHAR(3),
FOREIGN KEY (EmpId) REFERENCES Abu_Employee(EmpId));
Desc EmpSalary

Output:

CREATE TABLE EmpProject (


EmpId INT,
ProjectId VARCHAR(5),
ClientId VARCHAR(5),
StartYear INT,
EndYear INT,
FOREIGN KEY (EmpId) REFERENCES Abu_Employee(EmpId),
FOREIGN KEY (ProjectId) REFERENCES Project(ProjectId),
FOREIGN KEY (ClientId) REFERENCES ClientTable(ClientId));

Output:

32 | P a g e
15) Apply join concepts on above tables ?
Ans: select
EmpName, Salary
FROM Abu_Employee
INNER JOIN EmpSalary ON Abu_Employee.EmpId = EmpSalary.EmpId;
Output:

Select EmpName, ProjectId


from Abu_Employee
LEFT JOIN EmpProject ON Abu_Employee.EmpId = EmpProject.EmpId;

Output:

33 | P a g e
Select EmpName, IsPermanent
from EmpSalary
RIGHT JOIN Abu_Employee ON EmpSalary.EmpId = Abu_Employee.EmpId;

Output:

Select EmpName, ProjectId,StartYear


from Abu_Employee
FULL JOIN EmpProject ON Abu_Employee.EmpId = EmpProject.EmpId;

Output:

34 | P a g e
16) Select the department name of the company which is assigned to the employee whose
employee id is greater than 103 ?
Ans: SELECT DeptName
FROM Abu_Employee
JOIN EmpDept ON Abu_Employee.Department = EmpDept.DeptId
WHERE Abu_Employee.EmpId > 103;

Output:

17) Select the name of the employee who is working under Abhishek ?
Ans: SELECT EmpName
FROM Abu_Employee
WHERE EmpHeadId = (SELECT EmpId FROM Abu_Employee WHERE EmpName =
'Abhishek');

Output:

35 | P a g e
18) Select the name of the employee who is department head of HR ?
Ans: SELECT EmpName
FROM Abu_Employee
WHERE EmpId = (SELECT DeptHead FROM EmpDept WHERE DeptName = 'HR');

Output:

19) Select the name of the employee head who is permanent ?


Ans: SELECT EmpName
FROM Abu_Employee
WHERE EmpId IN (SELECT EmpHeadId
FROM Abu_Employee WHERE EmpId IN (SELECT EmpId FROM EmpSalary WHERE
IsPermanent = 'Yes'));

Output:

36 | P a g e
20) Select the name and email of the department head who is not Permanent ?
Ans: SELECT EmpName, EmailId
FROM Abu_Employee
WHERE EmpId IN ( SELECT DeptHead FROM EmpDept) AND EmpId IN (SELECT EmpId
FROM EmpSalary WHERE IsPermanent = 'No');

Output:

21) Select the employee whose department off is Monday ?


Ans: SELECT EmpName
FROM Abu_Employee
JOIN EmpDept ON Abu_Employee.Department = EmpDept.DeptId
WHERE EmpDept.DeptOff = 'Monday';
Output:

37 | P a g e
22) Select the Indian clients details ?
Ans: SELECT ClientTable.ClientId, ClientTable.ClientName, ClientTable.cid, Country.cname
FROM ClientTable
JOIN Country ON ClientTable.cid = Country.cid
WHERE Country.cname = 'India';

Output:

23) Select the details of all employees working in development department ?


Ans: SELECT *
FROM Abu_Employee
JOIN EmpDept ON Abu_Employee.Department = EmpDept.DeptId
WHERE EmpDept.DeptName = 'Development';

Output:

38 | P a g e
24) Use the aggregate functions ?
Ans: SELECT COUNT(*) FROM Abu_Employee WHERE Department = 'E-101';
Output:

SELECT SUM(Salary) FROM EmpSalary WHERE IsPermanent = 'Yes';

Output:

SELECT AVG(Salary) FROM EmpSalary WHERE IsPermanent = 'Yes';

Output:

39 | P a g e
SELECT MIN(Salary) FROM EmpSalary WHERE IsPermanent = 'Yes';

Output:

SELECT EmpName, Salary AS Max_Salary


FROM Abu_Employee
JOIN EmpSalary ON Abu_Employee.EmpId = EmpSalary.EmpId
WHERE EmpSalary.Salary = (SELECT MAX(salary) FROM EmpSalary);

Output:

40 | P a g e

You might also like