0% found this document useful (0 votes)
127 views4 pages

MYSQL Queries

The document contains 30 questions asking to write SQL queries to retrieve data from database tables. The questions cover a wide range of SQL topics including aggregation, joins, filtering, sorting, limiting results and more. Sample queries are provided as answers to the questions.

Uploaded by

MADHAV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views4 pages

MYSQL Queries

The document contains 30 questions asking to write SQL queries to retrieve data from database tables. The questions cover a wide range of SQL topics including aggregation, joins, filtering, sorting, limiting results and more. Sample queries are provided as answers to the questions.

Uploaded by

MADHAV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Write a query to fetch the EmpFname from the EmployeeInfo table in upper case
and use the ALIAS name as Emp Name.

SELECT UPPER(EmpFname) AS EmpName FROM EmployeeInfo;


1
2. Q2. Write a query to fetch the number of employees working in the department
‘HR’.

1SELECT COUNT(*) FROM EmployeeInfo WHERE Department = 'HR';


3. Q3. Write a query to get the current date.

1SELECT GETDATE();

4. Write a query to retrieve the first four characters of  EmpLname from the
EmployeeInfo table.

1SELECT SUBSTRING(EmpLname, 1, 4) FROM EmployeeInfo;


5. Q5. Write a query to fetch only the place name(string before brackets) from the
Address column of EmployeeInfo table.
6.

1SELECT MID(Address, 0, LOCATE('(',Address)) FROM EmployeeInfo;


Using SUBSTRING

SELECT SUBSTRING(Address, 1, CHARINDEX('(',Address)) FROM EmployeeInfo;


1
7. Q6. Write a query to create a new table which consists of data and structure copied
from the other table.

Using the SELECT INTO command:

1SELECT * INTO NewTable FROM EmployeeInfo WHERE 1 = 0;


Using the CREATE command in MySQL:

8. MySQL DBA Certific

1CREATE TABLE NewTable AS SELECT * FROM EmployeeInfo;


9. Q7. Write q query to find all the employees whose salary is between 50000 to
100000.

1SELECT * FROM EmployeePosition WHERE Salary BETWEEN '50000' AND '100000';


10. Q8. Write a query to find the names of employees that begin with ‘S’

1SELECT * FROM EmployeeInfo WHERE EmpFname LIKE 'S%';


11. Q9. Write a query to fetch top N records.

1SELECT TOP N * FROM EmployeePosition ORDER BY Salary DESC;


By using the LIMIT command in MySQL:

Page 1 of 4 Reference ID: PDM-Document number


1SELECT * FROM EmpPosition ORDER BY Salary DESC LIMIT N;
12. Q10. 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.

1SELECT CONCAT(EmpFname, ' ', EmpLname) AS 'FullName' FROM EmployeeInfo;


13. Q11. Write a query find number of employees whose DOB is between 02/05/1970
to 31/12/1975 and are grouped according to gender

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.

1SELECT * FROM EmployeeInfo ORDER BY EmpFname desc, Department asc;


15. Q13. 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.

1SELECT * FROM EmployeeInfo WHERE EmpLname LIKE '____a';


16. Q14. Write a query to fetch details of all employees excluding the employees with
first names, “Sanjay” and “Sonia” from the EmployeeInfo table.

1SELECT * FROM EmployeeInfo WHERE EmpFname NOT IN ('Sanjay','Sonia');

17. Write a query to fetch details of employees with the address as “DELHI(DEL)”.

1SELECT * FROM EmployeeInfo WHERE Address LIKE 'DELHI(DEL)%';


18. Q16. Write a query to fetch all employees who also hold the managerial position.

1SELECT E.EmpFname, E.EmpLname, P.EmpPosition


2FROM EmployeeInfo E INNER JOIN EmployeePosition P ON
3E.EmpID = P.EmpID AND P.EmpPosition IN ('Manager');
19. Q17. Write a query to fetch the department-wise count of employees sorted by
department’s count in ascending order.

1SELECT Department, count(EmpID) AS EmpDeptCount


2FROM EmployeeInfo GROUP BY Department
3ORDER BY EmpDeptCount ASC;
20. Q18. 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:

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.

Page 2 of 4 Reference ID: PDM-Document number


1SELECT * FROM EmployeeInfo E
2WHERE EXISTS
3(SELECT * FROM EmployeePosition P WHERE E.EmpId = P.EmpId);
22. Q20. Write a query to retrieve two minimum and maximum salaries from the
EmployeePosition table.

To retrieve two minimum salaries, you can write a query as below:

1SELECT DISTINCT Salary FROM EmployeePosition E1


2 WHERE 2 >= (SELECTCOUNT(DISTINCT Salary)FROM EmployeePosition E2
3  WHERE E1.Salary >= E2.Salary) ORDER BY E1.Salary DESC;
To retrieve two maximum salaries, you can write a query as below:
1SELECT DISTINCT Salary FROM EmployeePosition E1
2 WHERE 2 >= (SELECTCOUNT(DISTINCT Salary) FROM EmployeePosition E2
3  WHERE E1.Salary <= E2.Salary) ORDER BY E1.Salary DESC;
23. Q21. Write a query to find the Nth highest salary from the table without using
TOP/limit keyword.

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.

1SELECT EmpID, EmpFname, Department COUNT(*)


2FROM EmployeeInfo GROUP BY EmpID, EmpFname, Department
3HAVING COUNT(*) > 1;
25. Q23. Write a query to retrieve the list of employees working in the same
department.

1Select DISTINCT E.EmpID, E.EmpFname, E.Department


2FROM EmployeeInfo E, Employee E1
3WHERE E.Department = E1.Department AND E.EmpID != E1.EmpID;
26. Q24. Write a query to retrieve the last 3 records from the EmployeeInfo table.

1SELECT * FROM EmployeeInfo WHERE


2EmpID <=3 UNION SELECT * FROM
3(SELECT * FROM EmployeeInfo E ORDER BY E.EmpID DESC)
4AS E1 WHERE E1.EmpID <=3;
27. Q25. Write a query to find the third-highest salary from the EmpPosition table.

1SELECT TOP 1 salary


2FROM(
3SELECT TOP 3 salary
4FROM employee_table
5ORDER BY salary DESC) AS emp
6ORDER BY salary ASC;
28. Q26. 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:

Page 3 of 4 Reference ID: PDM-Document number


1SELECT * 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:

1SELECT * FROM EmployeeInfo WHERE EmpID = (SELECT MAX(EmpID) FROM EmployeeInfo);


29. Q27. Write a query to add email validation to your database

1SELECT Email FROM EmployeeInfo WHERE NOT REGEXP_LIKE(Email, ‘[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4


30. Q28. Write a query to retrieve Departments who have less than 2 employees
working in it.

1SELECT DEPARTMENT, COUNT(EmpID) as 'EmpNo' FROM EmployeeInfo GROUP BY DEPARTMENT HAVING


31. Q29. Write a query to retrieve EmpPostion along with total salaries paid for each
of them.

1SELECT EmpPosition, SUM(Salary) from EmployeePosition GROUP BY EmpPosition;


32. Q30. Write a query to fetch 50% records from the EmployeeInfo table.

1SELECT *
2FROM EmployeeInfo WHERE
3EmpID <= (SELECT COUNT(EmpID)/2 from EmployeeInfo);

Page 4 of 4 Reference ID: PDM-Document number

You might also like