SQL

SQL Queries – Basic to Advanced

In this article on SQL Queries – Basic to Advanced, I will discuss how to formulate SQL queries.

The following section specifies the database tables that we use for the queries. Also, we create the tables on Oracle 11g Database

Create the following tables.

To begin with, we need to create two tables – EmpDetails, and EmpDesignation. The following tables provide the schema of these two tables respectively.

  • EmpDetails
Field NameType
EmpId (Primary Key)Integer
EmpNameVarchar(20)
DepartmentVarchar(10)
LocationVarchar(20)
DOBDate
GenderVarchar(1)
Date of JoiningDate
SalaryInteger
Schema of the EmpDetails Table
  • EmpDesignation
Field NameType
D_id (Primary Key)Integer
EmpId (Foreign Key)Integer
DesignationVarchar(20)
ProjectVarchar(2)
Schema of the EmpDesignation Table

After that Insert the following rows.

  • EmpDetails
EmpIdEmpNameDepartm entLocationDOBGen derDateOfJoini ngSalary
10Arush AggarwalHRHyderabad (HYD)01-12-1976M01-05-202275000
20Sumit ChaudharyAdminDelhi (DEL)01-01-1968M15-05-202225000
30Riya KhannaAccountMumbai (MUM)23-05-1970F15-05-202245000
40PriyaHRBangalore (BNG)15-12-1990F01-05-202228000
50Anmol SinghHRMumbai (MUM)18-01-2001M02-05-202234000
60AvinashAdminDelhi (DEL)05-05-2001M01-05-202230000
70Mrinal PandeyTechnicalDelhi (DEL)12-01-1996F15-05-202235000
80AnuragHRBangalore (BNG)15-05-1992M01-05-202020000
90RichaHRBangalore (BNG)10-01-1998F01-05-202020000
100Sapna GuptaTechnicalDelhi (DEL)12-09-1998F01-05-202240000
110PranjalTechnicalDelhi (DEL)20-06-1998F01-05-202242000
120Amit KumarAccountDelhi (DEL)12-07-2005M15-05-202215000
130RaghavAccountDelhi (DEL)30-09-2006M15-05-202215000
140VinayAdminMumbai (MUM)01-08-2001M01-05-202020000
150Ashish KumarAdminMumbai (MUM)17-02-1997M01-05-202020000
Sample Data for the EmpDetails Table

Similarly, add some records in the EmpDesignation Table.

  • EmpDesignation
D_idEmpIdDesignationProject
110ManagerP1
220ExecutiveP2
330LeadP2
420ManagerP3
510ExecutiveP3
610ManagerP4
740ExecutiveP4
850ExecutiveP4
960ExecutiveP5
1070ExecutiveP5
1110ManagerP5
12100ExecutiveP4
13110ExecutiveP4
1410LeadP4
1520LeadP4
Sample Data for the EmpDesignation Table

Further, perform the following SQL Queries – Basic to Advanced on the above tables.

  1. Find the average salary of employees.
  2. Also, Show the department name and the average salary for each department
  3. Similarly, get distinct projects from the EmpDesignation table without using distinct keyword.
  4. Find the second highest salary along with Employee’s name.
  5. Also, Fetch the list of employees with the same salary.
  6. Show all departments along with the number of people in there.
  7. Fetch the last five records from the EmpDetails table.
  8. Similarly, fetch three max salaries from the EmpDetails table.
  9. Fetch the first 50% of records from the EmpDesignation table.
  10. Also, fetch the 5th highest salary without using the TOP or limit method.
  11. Display the complete details of the managers who are also executives.
  12. Show complete details of employees whose DOB is between 01/01/1990 to 31/12/1999 and are grouped according to gender
  13. Fetch details of all employees excluding the employees whose name starts with ‘A’.
  14. Retrieve employee details from the EmpDetails table who have not been assigned any project.
  15. Also, retrieve employee details from the EmpDetails table who is working on at least one project.
  16. Retrieve employee details from the EmpDetails table who are working on maximum projects.
  17. Display complete details of employees group by the project.
  18. Display the departments where employees have joined earliest but are not working on any project.
  19. Show projects which have the highest executives working on them.
  20. Create a Sequence DesignationSequence with minimum value=1000, maximum value=10000, and increment=100.

At first, we need to log in with Oracle username and password as shown below.

Oracle 11 g for Running SQL Queries - Basic to Advanced
Oracle 11 g for Running SQL Queries – Basic to Advanced

Further Reading

SQL Practice Exercise

Princites

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *