Hello,
What are the commonly used SQL Queries using Employee Table?
Will be more helpful if you can give some examples with data.
I will use the below employee table to write some queries and show the expected results so that you will be able to understand very well.
Below is my Employee table.
Emp ID |
First_Name |
Salary |
Department |
1 |
Anandh |
5000 |
AAA |
2 |
Anjali |
7000 |
BBB |
3 |
Arun |
8500 |
ABC |
4 |
Deepak |
15000 |
AAA |
5 |
John |
22000 |
CDN |
6 |
Monalisa |
28000 |
KSN |
7 |
Sandhya |
32000 |
AAA |
8 |
Rohan |
40000 |
NND |
Query:
1) How to Increase the income of all employees by 5% in a table?
SQL: Update Employee SET Income=Income+(Income*5.0/100)
Expected Results:
Emp ID |
First_Name |
Salary |
Department |
1 |
Anandh |
5250 |
AAA |
2 |
Anjali |
7350 |
BBB |
3 |
Arun |
8925 |
ABC |
4 |
Deepak |
15750 |
AAA |
5 |
John |
23100 |
CDN |
6 |
Monalisa |
29400 |
KSN |
7 |
Sandhya |
33600 |
AAA |
8 |
Rohan |
42000 |
NND |
Query
2) Find the name of an employee starting with 'A'?
SQL: Select first_name From Employees where first_name Like 'A%'
Expected Results:
Emp ID |
First_Name |
Salary |
Department |
1 |
Anandh |
5000 |
AAA |
2 |
Anjali |
7000 |
BBB |
3 |
Arun |
8500 |
ABC |
Query:
3) Find the number of employees working in the department 'ABC'
SQL: Select Count(*) from employees where department_name='ABC'
Expected Results:
Emp ID |
First_Name |
Salary |
Department |
1 |
Arun |
8500 |
ABC |
Query
4) Print Details of employees whose first name ends with 'A' and contains 5 Alphabets
SQL: Select * from Employees where First_Name Like '_ _ _ _ a'
Expected Results:
Emp ID |
First_Name |
Salary |
Department |
1 |
Monalisa |
28000 |
KSN |
2 |
Sandhya |
32000 |
AAA |
Query
5) Print details of an employee whose salary lies between 5000 and 25000
SQL: Select * from Employee Where Salary BETWEEN 5000 and 25000
Expected Results:
Emp ID |
First_Name |
Salary |
Department |
1 |
Anandh |
5000 |
AAA |
2 |
Anjali |
7000 |
BBB |
3 |
Arun |
8500 |
ABC |
4 |
Deepak |
15000 |
AAA |
5 |
John |
22000 |
CDN |
Here are the TOP 5 SQL Queries using Employee Table with Answers.
1) Fetch emp_name in upper case and use alias
Select Emp_Name AS Upper(emp First_Name)
From
Employees
2) Fetch top N records
SELECT TOP N * FROM EMPLOYEES
ORDER BY SALARY DESC
3) Retrieve Employee First Name and Last Name in Single Column as Full Name and First Name and Last Name Seperate with space.
SELECT CONCAT(EMP_FIRSTNAME, ` ´, EMP_LASTNAME)
AS FULL_NAME
FROM EMPLOYEES;
4) Retrieve employee position along with total salaries paid for each of them
SELECT EMP_POSITION,
SUM(SALARY )
FROM
EMPLOYEES
GROUP BY
EMPLOYEE_POSITION;
5) Fetch details of employees with address as 'MUMBAI(MUM)'
SELECT * FROM EMPLOYEES
WHERE ADDRESS LIKE ' MUMBAI(MUM)'