MYSQL Queries
MYSQL Queries
Write a query to fetch the EmpFname from the EmployeeInfo table in upper case
and use the ALIAS name as Emp Name.
1SELECT GETDATE();
4. Write a query to retrieve the first four characters of EmpLname from the
EmployeeInfo table.
1SELECT COUNT(*), Gender FROM EmployeeInfo WHERE DOB BETWEEN '02/05/1970 ' AND '31/12/1975' G
14. Q12. 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.
To fetch details mathcing a certain value, you have to use the LIKE operator in SQL.
17. Write a query to fetch details of employees with the address as “DELHI(DEL)”.
To retrieve the even records from a table, you have to use the MOD() function as follows:
1SELECT 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:
1SELECT EmpID FROM (SELECT rowno, EmpID from EmployeeInfo) WHERE MOD(rowno,2)=1;
21. Q19. Write a SQL query to retrieve employee details from EmployeeInfo table who
have a date of joining in the EmployeePosition table.
1SELECT Salary
2FROM EmployeePosition E1
3WHERE N-1 = (
4 SELECT COUNT( DISTINCT ( E2.Salary ) )
5 FROM EmployeePosition E2
6 WHERE E2.Salary > E1.Salary );
24. Q22. Write a query to retrieve duplicate records from a table.
To display the first record from the EmployeeInfo table, you can write a query as follows:
1SELECT *
2FROM EmployeeInfo WHERE
3EmpID <= (SELECT COUNT(EmpID)/2 from EmployeeInfo);