Photo by Jordan Harrison on Unsplash
How to group data , Sub-queries & questions: Enhancing(SQL) (Part-4)
Table of contents
- PRE-REQUISTES
- Let's Create a table for upcoming problems.
- QUERY ๐
- Now it's time to insert some data
- QUERY ๐
- Q.1 Write a SQL query to find out the maximum salary from Employee Table?
- Q.1.1 What should be the Sub-Query for this ๐ type of question?
- QUERY ๐
- Q.2 Display the name of the employee who is taking highest salary?
- QUERY ๐
- Q.3 Write a SQL query to find out the second highest salary from Employee Table?
- QUERY ๐
- Q.4 Write a SQL query to find out the second highest salary from Employee Table and fetch its Employee Name?
- QUERY ๐
- Q.5 Write a query to display all the DEPARTMENT names along with the employees working in that DEPARTMENT?
- Q.5.1 What should be the Condition or Query for this ๐ type of question?
- Q.5.2 How to Group Data?
- QUERY ๐
- QUERY ๐
- <------------END-------------->
PRE-REQUISTES
PART-1 (Understand concept of all keys (theory) and what are anomalies in database)
PART-2 (CRUD operations in database , learn syntax & understand concept by doing)
PART-3 (1st Normal form , 2NF , 3NF , Partial Dependency , Transitive Dependency)
Let's Create a table for upcoming problems.
QUERY ๐
create table employee (emp_id int ,emp_name varchar(20) ,department varchar(20) ,salary int);
Now it's time to insert some data
QUERY ๐
insert into employee values(1,'Alex','IT',10000),(2,'Mathew','HR',20000),(3,'John','Finance',30000),(4,'Rodger','HR',40000),(5,'Nick','IT',50000);
Table Created Successfully.
EMPLOYEE TABLE ๐
EMP_ID | EMP_NAME | DEPARTMENT | SALARY |
1 | Alex | IT | 10000 |
2 | Mathew | HR | 20000 |
3 | John | Finance | 30000 |
4 | Rodger | HR | 40000 |
5 | Nick | IT | 50000 |
Q.1 Write a SQL query to find out the maximum salary from Employee Table?
Let's first read the question again what it is trying to say . Simply it's saying to fetch the number which is the greatest among all salaries .
Q.1.1 What should be the Sub-Query for this ๐ type of question?
Remember whenever this type of questions appears where you want to find SUM , AVERAGE , MAXIMUM , MINIMUM or COUNT then these are known as Aggregate function in SQL. An aggregate function in SQL performs a calculation on multiple values and returns a single value.
QUERY ๐
SELECT MAX(SALARY) FROM EMPLOYEE;
Q.2 Display the name of the employee who is taking highest salary?
EMP_ID | EMP_NAME | DEPARTMENT | SALARY |
1 | Alex | IT | 10000 |
2 | Mathew | HR | 20000 |
3 | John | Finance | 30000 |
4 | Rodger | HR | 40000 |
5 | Nick | IT | 50000 |
Again question is similar but here , concept of sub-query will be use.
QUERY ๐
SELECT EMP_NAME FROM EMPLOYEE WHERE SALARY = (SELECT MAX(SALARY) FROM EMPLOYEE);
Q.3 Write a SQL query to find out the second highest salary from Employee Table?
Again , similar to 1st type of question just we have to find out the second highest salary. How can I achieve that ?? simple I will just write a sub-query in which I will fetch the data without the maximum salary . Let me show you how?
EMP_ID | EMP_NAME | DEPARTMENT | SALARY |
1 | Alex | IT | 10000 |
2 | Mathew | HR | 20000 |
3 | John | Finance | 30000 |
4 | Rodger | HR | 40000 |
5 | Nick | IT | 50000 |
QUERY ๐
SELECT MAX(SALARY) FROM EMPLOYEE WHERE SALARY <> (SELECT MAX(SALARY) FROM EMPLOYEE);
NOTE: '<>' is a not equal operator which is similar to '=!' .
Q.4 Write a SQL query to find out the second highest salary from Employee Table and fetch its Employee Name?
This question is similar to Q.3 but here we have to find the Employee Name instead of SALARY . I will simply use last query as a Sub-query. Let me show you how?
EMP_ID | EMP_NAME | DEPARTMENT | SALARY |
1 | Alex | IT | 10000 |
2 | Mathew | HR | 20000 |
3 | John | Finance | 30000 |
4 | Rodger | HR | 40000 |
5 | Nick | IT | 50000 |
QUERY ๐
SELECT EMP_NAME FROM EMPLOYEE_TABLE WHERE SALARY = (SELECT MAX(SALARY) FROM EMPLOYEE WHERE SALARY <> (SELECT MAX(SALARY) FROM EMPLOYEE));
Q.5 Write a query to display all the DEPARTMENT names along with the employees working in that DEPARTMENT?
Let's first read the question again what it is trying to say . Simply it's saying to fetch the number which is the greatest among all salaries .
Q.5.1 What should be the Condition or Query for this ๐ type of question?
For this question I am using something different which I didn't told yet . Group By its nothing but I can group many values together . Let me understand you all by solution .The GROUP BY
statement groups rows that have the same values into summary rows, like "find the number of customers in each country".
The GROUP BY
statement is often used with aggregate functions (COUNT()
, MAX()
, MIN()
, SUM()
, AVG()
) to group the result-set by one or more columns.
AIM : My Aim to find the all departments along with the number of people working in those departments . Now here I want to fetch 2 things simultaneously.
Q.5.2 How to Group Data?
First I am showing you folks how to group data and this would be the half part of question.
QUERY ๐
select department from employee group by department;
Now Grouping is completed just number of employees is left. So I have to run another query. Now only I have to use Count() function and you all know very well that its an aggregate function.
QUERY ๐
select department,count(department) from employee group by department;