Queries With Table
Queries With Table
EmployeePosition Table:
2. Write a query to fetch the number of employees working in the department ‘HR’.
TABLE
3. Write a query to get the current date.
SELECT GETDATE();
SELECT SYSTDATE();
4. Write a query to retrieve the first four characters of EmpLname from the EmployeeInfo table.
SELECT SUBSTRING(EmpLname, 1, 4) FROM EmployeeInfo;
5. Write a query to fetch only the place name (string before brackets) from the Address column of
EmployeeInfo table.
SELECT MID(Address, 0, LOCATE('(',Address)) FROM EmployeeInfo;
SELECT SUBSTRING(Address, 1, CHARINDEX('(',Address)) FROM EmployeeInfo;
6. Write a query to create a new table which consists of data and structure copied from the other table.
7. Write q query to find all the employees whose salary is between 50000 to 100000.
SELECT * FROM EmployeePosition WHERE Salary BETWEEN '50000' AND '100000';
8. Write a query to find the names of employees that begin with ‘S’
10. Write a query to retrieve the EmpFname and EmpLname in a single column as “FullName”. The first
name and the last name must be separated with space.
SELECT COUNT(*), Gender FROM EmployeeInfo WHERE DOB BETWEEN '02/05/1970 ' AND '31/12/1975'
GROUP BY Gender;
12. Write a query to fetch all the records from the EmployeeInfo table ordered by EmpLname in descending
order and Department in the ascending order.
To order the records in ascending and descnding order, you have to use the ORDER BY statement in SQL. TABLE
SELECT * FROM EmployeeInfo ORDER BY EmpFname desc, Department asc;
13. Write a query to fetch details of employees whose EmpLname ends with an alphabet ‘A’ and contains
five alphabets.
To fetch details mathcing a certain value, you have to use the LIKE operator in SQL.
SELECT * FROM EmployeeInfo WHERE EmpLname LIKE '____a';
14. Write a query to fetch details of all employees excluding the employees with first names, “Sanjay” and
“Sonia” from the EmployeeInfo table.
15. Write a query to fetch details of employees with the address as “DELHI(DEL)”.
16. Write a query to fetch all employees who also hold the managerial position. TABLE
SELECT E.EmpFname, E.EmpLname, P.EmpPosition
FROM EmployeeInfo E INNER JOIN EmployeePosition P ON
E.EmpID = P.EmpID AND P.EmpPosition IN ('Manager');
17. Write a query to fetch the department-wise count of employees sorted by department’s count in
ascending order.
SELECT Department, count(EmpID) AS EmpDeptCount
FROM EmployeeInfo GROUP BY Department
ORDER BY EmpDeptCount ASC;
18. Write a query to calculate the even and odd records from a table.
To retrieve the even records from a table, you have to use the MOD() function as follows:
SELECT EmpID FROM (SELECT rowno, EmpID from EmployeeInfo) WHERE MOD(rowno,2)=0;
Similarly, to retrieve the odd records from a table, you can write a query as follows:
SELECT EmpID FROM (SELECT rowno, EmpID from EmployeeInfo) WHERE MOD(rowno,2)=1;
19. Write a SQL query to retrieve employee details from EmployeeInfo table who have a date of joining in
the EmployeePosition table.
SELECT * FROM EmployeeInfo E WHERE EXISTS (SELECT * FROM EmployeePosition P WHERE E.EmpId = TABLE
P.EmpId);
20. Write a query to retrieve two minimum and maximum salaries from the EmployeePosition table.
To retrieve two minimum salaries:
SELECT DISTINCT Salary FROM EmployeePosition E1 WHERE 2 >= (SELECTCOUNT(DISTINCT Salary)FROM
EmployeePosition E2 WHERE E1.Salary >= E2.Salary) ORDER BY E1.Salary DESC;
To retrieve two maximum salaries:
SELECT DISTINCT Salary FROM EmployeePosition E1 WHERE 2 >= (SELECTCOUNT(DISTINCT Salary) FROM
EmployeePosition E2 WHERE E1.Salary <= E2.Salary) ORDER BY E1.Salary DESC;
21. Write a query to find the Nth highest salary from the table without using TOP/limit keyword.
SELECT Salary
FROM EmployeePosition E1
WHERE N-1 = (
SELECT COUNT( DISTINCT ( E2.Salary ) )
FROM EmployeePosition E2
WHERE E2.Salary > E1.Salary );
23. Write a query to retrieve the list of employees working in the same department.
Select DISTINCT E.EmpID, E.EmpFname, E.Department
FROM EmployeeInfo E, Employee E1
WHERE E.Department = E1.Department AND E.EmpID != E1.EmpID;
24. Write a query to retrieve the last 3 records from the EmployeeInfo table.
SELECT * FROM EmployeeInfo WHERE
EmpID <=3 UNION SELECT * FROM
(SELECT * FROM EmployeeInfo E ORDER BY E.EmpID DESC)
AS E1 WHERE E1.EmpID <=3;
25. Write a query to find the third-highest salary from the EmpPosition table.
SELECT TOP 1 salary FROM( SELECT TOP 3 salary FROM employee_table ORDER BY salary DESC) AS emp
ORDER BY salary ASC; TABLE
26. Write a query to display the first and the last record from the EmployeeInfo table.
To display the first record from the EmployeeInfo table, you can write a query as follows:
SELECT * FROM EmployeeInfo WHERE EmpID = (SELECT MIN(EmpID) FROM EmployeeInfo);
To display the last record from the EmployeeInfo table, you can write a query as follows:
SELECT * FROM EmployeeInfo WHERE EmpID = (SELECT MAX(EmpID) FROM EmployeeInfo);
27. Write a query to add email validation to your database
SELECT Email FROM EmployeeInfo WHERE NOT REGEXP_LIKE(Email, ‘[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]
{2,4}’, ‘i’);
28. Write a query to retrieve Departments who have less than 2 employees working in it.
TABLE
Q29. Write a query to retrieve EmpPostion along with total salaries paid for each of them.
SELECT EmpPosition, SUM(Salary) from EmployeePosition GROUP BY EmpPosition;
Q30. Write a query to fetch 50% records from the EmployeeInfo table.
SELECT * FROM EmployeeInfo WHERE
EmpID <= (SELECT COUNT(EmpID)/2 from EmployeeInfo);
EMPLOYEE
EMP_ID EMP_NAME CITY SALARY AGE
1 Angelina Chicago 200000 30
2 Robert Austin 300000 26
3 Christian Denver 100000 42
4 Kristen Washington 500000 29
5 Russell Los angels 200000 36
6 Marry Canada 600000 48
EMPLOYEE POSITION
PROJECT
PROJECT_NO EMP_ID DEPARTMENT EMP_ID POSITION DEPARTMENT
1 CEO BDA
101 1 Testing
102 2 Development 3 Manager Designing
103 3 Designing 4 Executive Testing
104 4 Development 6 Developer Development
1. Write a query to retrieve all the employee working on current project
3. Query the data to know the position of employee working on current project
5. Fetch all the employee details and employee position whose age is more than 30