SQL Final
SQL Final
1|Page
                                            Table of Contents
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 abc@gmail.com.             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;
      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.
       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
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:
      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
Output:
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:
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
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
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
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:
20 | P a g e
CREATE TABLE EmpDept (
DeptId VARCHAR(10) PRIMARY KEY,
DeptName VARCHAR(50),
DeptOff VARCHAR(20),
DeptHead INT);
desc EmpDept
Output:
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:
22 | P a g e
CREATE TABLE Project (
ProjectId VARCHAR(5) PRIMARY KEY,
Duration INT);
desc Project
Output:
23 | P a g e
CREATE TABLE Country (
cid VARCHAR(5) PRIMARY KEY,
cname VARCHAR(20));
Desc Country;
Output:
24 | P a g e
CREATE TABLE ClientTable (
ClientId VARCHAR(5) PRIMARY KEY,
ClientName VARCHAR(20),
cid VARCHAR(5));
Desc ClientTable;
Output:
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:
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:
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:
Output:
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:
Output:
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 abc@gmail.com ?
Ans: UPDATE Abu_Employee
      SET EmailId = 'abc@gmail.com'
     where EmpName = 'Rahul';
     SELECT * FROM Abu_Employee;
Output:
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:
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:
Output:
33 | P a g e
Select EmpName, IsPermanent
from EmpSalary
RIGHT JOIN Abu_Employee ON EmpSalary.EmpId = Abu_Employee.EmpId;
Output:
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:
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:
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:
Output:
38 | P a g e
    24) Use the aggregate functions ?
Ans: SELECT COUNT(*) FROM Abu_Employee WHERE Department = 'E-101';
Output:
Output:
Output:
39 | P a g e
SELECT MIN(Salary) FROM EmpSalary WHERE IsPermanent = 'Yes';
Output:
Output:
40 | P a g e