DEV Community

Cover image for Top 10 SQL Queries for Freshers πŸ“
Jemmy Dalsaniya
Jemmy Dalsaniya

Posted on

Top 10 SQL Queries for Freshers πŸ“

  • SQL Query to find the second highest salary of Employee:

-> SELECT MAX(Salary) FROM Employee WHERE Salary NOT IN (select MAX(Salary) from Employee );

  • Write SQL Query to display the current date?

-> SELECT GetDate();

  • Write an SQL Query to find the year from date.

-> SELECT YEAR(GETDATE()) as "Year"

  • Write a query to fetch values in table test_a that are and not in test_b without using the NOT keyword.

-> select * from test_a except select * from test_b;

select a.id from test_a a left join test_b b on a.id = b.id where b.id is null;

  • Write a SQL query to find the 10th highest employee salary from an Employee table.

-> SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 9,1;

SELECT Salary FROM (SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 10) AS Emp ORDER BY Salary LIMIT 1;

SELECT * from Employee as e1 where n-1 = (Select count(distinct Salary) from Employee as e2 where e1.Salary < e2.Salary)

  • Write an SQL query to fetch three max GPA from a table using co-related subquery.

-> SELECT DISTINCT GPA FROM Student S1 WHERE 3 >= (SELECT COUNT(DISTINCT GPA) FROM Student S2 WHERE S1.GPA <= S2.GPA) ORDER BY S1.GPA DESC;

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

-> CREATE TABLE NewTable AS SELECT * FROM EmployeeInfo;

  • Write an SQL query to fetch the last five records from a table.

-> SELECT * FROM (SELECT * FROM Student ORDER BY STUDENT_ID DESC LIMIT 5
) AS subquery ORDER BY STUDENT_ID;

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

-> SELECT * FROM EmployeeInfo WHERE EmpID <= (SELECT COUNT(EmpID)/2 from EmployeeInfo);

Top comments (0)