SQL Questions
SQL Questions
Worker
Bonus
Sample Table – Title
Title
To prepare the sample data, you can run the following queries in your
database query executor or on the SQL command line. We’ve tested
them with MySQL Server 5.7 and MySQL Workbench 6.3.8 query
browser. You can also download these tools and install them to execute
the SQL queries.
Q-26. Write an SQL query to show only odd rows from a table.
Ans.
Q-30. Write an SQL query to show records from one table that
another table does not have.
Ans.
Q-31. Write an SQL query to show the current date and time.
Ans.
The following MySQL query returns the current date:
SELECT CURDATE();
Whereas the following MySQL query returns the current date and time:
SELECT NOW();
Here is a SQL Server query that returns the current date and time:
SELECT getdate();
Find this Oracle query that also returns the current date and time:
Q-32. Write an SQL query to show the top n (say 10) records of
a table.
Ans.
MySQL query to return the top n records using the LIMIT method:
The following query is using the correlated subquery to return the 5th
highest salary:
SELECT Salary
FROM Worker W1
WHERE 4 = (
SELECT COUNT( DISTINCT ( W2.Salary ) )
FROM Worker W2
WHERE W2.Salary >= W1.Salary
);
Use the following generic method to find the nth highest salary without
using TOP or limit.
SELECT Salary
FROM Worker W1
WHERE n-1 = (
SELECT COUNT( DISTINCT ( W2.Salary ) )
FROM Worker W2
WHERE W2.Salary >= W1.Salary
);
Q-39. Write an SQL query to fetch the first 50% of records from
a table.
Ans.
SELECT *
FROM WORKER
WHERE WORKER_ID <= (SELECT count(WORKER_ID)/2 from Worker);
Practicing SQL query interview questions is a great way to improve your
understanding of the language and become more proficient in using it.
However, in addition to improving your technical skills, practicing SQL
query questions can also help you advance your career. Many
employers are looking for candidates who have strong SQL skills, so
being able to demonstrate your proficiency in the language can give
you a competitive edge.
Q-42. Write an SQL query to show the last record from a table.
Ans.
The following query will return the last record from the Worker table:
Q-44. Write an SQL query to fetch the last five records from a
table.
Ans.