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 Name | Type |
EmpId (Primary Key) | Integer |
EmpName | Varchar(20) |
Department | Varchar(10) |
Location | Varchar(20) |
DOB | Date |
Gender | Varchar(1) |
Date of Joining | Date |
Salary | Integer |
- EmpDesignation
Field Name | Type |
D_id (Primary Key) | Integer |
EmpId (Foreign Key) | Integer |
Designation | Varchar(20) |
Project | Varchar(2) |
After that Insert the following rows.
- EmpDetails
EmpId | EmpName | Departm ent | Location | DOB | Gen der | DateOfJoini ng | Salary |
10 | Arush Aggarwal | HR | Hyderabad (HYD) | 01-12-1976 | M | 01-05-2022 | 75000 |
20 | Sumit Chaudhary | Admin | Delhi (DEL) | 01-01-1968 | M | 15-05-2022 | 25000 |
30 | Riya Khanna | Account | Mumbai (MUM) | 23-05-1970 | F | 15-05-2022 | 45000 |
40 | Priya | HR | Bangalore (BNG) | 15-12-1990 | F | 01-05-2022 | 28000 |
50 | Anmol Singh | HR | Mumbai (MUM) | 18-01-2001 | M | 02-05-2022 | 34000 |
60 | Avinash | Admin | Delhi (DEL) | 05-05-2001 | M | 01-05-2022 | 30000 |
70 | Mrinal Pandey | Technical | Delhi (DEL) | 12-01-1996 | F | 15-05-2022 | 35000 |
80 | Anurag | HR | Bangalore (BNG) | 15-05-1992 | M | 01-05-2020 | 20000 |
90 | Richa | HR | Bangalore (BNG) | 10-01-1998 | F | 01-05-2020 | 20000 |
100 | Sapna Gupta | Technical | Delhi (DEL) | 12-09-1998 | F | 01-05-2022 | 40000 |
110 | Pranjal | Technical | Delhi (DEL) | 20-06-1998 | F | 01-05-2022 | 42000 |
120 | Amit Kumar | Account | Delhi (DEL) | 12-07-2005 | M | 15-05-2022 | 15000 |
130 | Raghav | Account | Delhi (DEL) | 30-09-2006 | M | 15-05-2022 | 15000 |
140 | Vinay | Admin | Mumbai (MUM) | 01-08-2001 | M | 01-05-2020 | 20000 |
150 | Ashish Kumar | Admin | Mumbai (MUM) | 17-02-1997 | M | 01-05-2020 | 20000 |
Similarly, add some records in the EmpDesignation Table.
- EmpDesignation
D_id | EmpId | Designation | Project |
1 | 10 | Manager | P1 |
2 | 20 | Executive | P2 |
3 | 30 | Lead | P2 |
4 | 20 | Manager | P3 |
5 | 10 | Executive | P3 |
6 | 10 | Manager | P4 |
7 | 40 | Executive | P4 |
8 | 50 | Executive | P4 |
9 | 60 | Executive | P5 |
10 | 70 | Executive | P5 |
11 | 10 | Manager | P5 |
12 | 100 | Executive | P4 |
13 | 110 | Executive | P4 |
14 | 10 | Lead | P4 |
15 | 20 | Lead | P4 |
Further, perform the following SQL Queries – Basic to Advanced on the above tables.
- Find the average salary of employees.
- Also, Show the department name and the average salary for each department
- Similarly, get distinct projects from the EmpDesignation table without using distinct keyword.
- Find the second highest salary along with Employee’s name.
- Also, Fetch the list of employees with the same salary.
- Show all departments along with the number of people in there.
- Fetch the last five records from the EmpDetails table.
- Similarly, fetch three max salaries from the EmpDetails table.
- Fetch the first 50% of records from the EmpDesignation table.
- Also, fetch the 5th highest salary without using the TOP or limit method.
- Display the complete details of the managers who are also executives.
- Show complete details of employees whose DOB is between 01/01/1990 to 31/12/1999 and are grouped according to gender
- Fetch details of all employees excluding the employees whose name starts with ‘A’.
- Retrieve employee details from the EmpDetails table who have not been assigned any project.
- Also, retrieve employee details from the EmpDetails table who is working on at least one project.
- Retrieve employee details from the EmpDetails table who are working on maximum projects.
- Display complete details of employees group by the project.
- Display the departments where employees have joined earliest but are not working on any project.
- Show projects which have the highest executives working on them.
- 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.
Further Reading
- AI
- Android
- Angular
- ASP.NET
- Augmented Reality
- AWS
- Bioinformatics
- Biometrics
- Blockchain
- Bootstrap
- C
- C#
- C++
- Cloud Computing
- Competitions
- Courses
- CSS
- Cyber Security
- Data Science
- Data Structures and Algorithms
- Data Visualization
- Datafication
- Deep Learning
- DevOps
- Digital Forensic
- Digital Trust
- Digital Twins
- Django
- Docker
- Dot Net Framework
- Drones
- Elasticsearch
- ES6
- Extended Reality
- Flutter and Dart
- Full Stack Development
- Git
- Go
- HTML
- Image Processing
- IoT
- IT
- Java
- JavaScript
- Kotlin
- Latex
- Machine Learning
- MEAN Stack
- MERN Stack
- Microservices
- MongoDB
- NodeJS
- PHP
- Power Bi
- Projects
- Python
- Quantum Computing
- React
- Robotics
- Rust
- Scratch 3.0
- Shell Script
- Smart City
- Software
- Solidity
- SQL
- SQLite
- Tecgnology
- Tkinter
- TypeScript
- VB.NET
- Virtual Reality
- Web Designing
- WebAssembly
- XML