SQL Test
SQL Test
•
It is a query language for all databases.
•
It is a programming language for SQL databases.
•
It is a query language for Oracle databases.
•
It is a programming language for PL databases.
•
True
•
False
• Theme Oriented
• Ordered
•
There are no commands to be executed.
•
The query result does not have any results.
•
The declaration has no variables.
•
The exception is not listed.
5.Microsoft SQL Server supports both SQL and its own query language
called _____.
• M-SQL
• T-SQL
• X-SQL
• R-SQL
Q-1. Write an SQL query to fetch “FIRST_NAME” from Worker table using
the alias name as <WORKER_NAME>.
Ans.
The required query is:
Ans.
The required query is:
Ans.
The required query is:
Ans.
The required query is:
Q-5. Write an SQL query to find the position of the alphabet (‘a’) in the
first name column ‘Amitabh’ from Worker table.
Ans.
The required query is:
Notes.
• The INSTR method is in case-sensitive by default.
• Using Binary operator will make INSTR work as the case-sensitive function.
Q-6. Write an SQL query to print the FIRST_NAME from Worker table after
removing white spaces from the right side.
Ans.
The required query is:
Ans.
The required query is:
Q-8. Write an SQL query that fetches the unique values of DEPARTMENT
from Worker table and prints its length.
Ans.
The required query is:
Q-9. Write an SQL query to print the FIRST_NAME from Worker table after
replacing ‘a’ with ‘A’.
Ans.
The required query is:
Q-10. Write an SQL query to print the FIRST_NAME and LAST_NAME from
Worker table into a single column COMPLETE_NAME. A space char
should separate them.
Ans.
The required query is:
Q-11. Write an SQL query to print all Worker details from the Worker table
order by FIRST_NAME Ascending.
Ans.
The required query is:
Q-12. Write an SQL query to print all Worker details from the Worker table
order by FIRST_NAME Ascending and DEPARTMENT Descending.
Ans.
The required query is:
Q-13. Write an SQL query to print details for Workers with the first name
as “Vipul” and “Satish” from Worker table.
Ans.
The required query is:
Ans.
The required query is:
Ans.
The required query is:
Ans.
The required query is:
Ans.
The required query is:
Ans.
The required query is:
Q-19. Write an SQL query to print details of the Workers whose SALARY
lies between 100000 and 500000.
Ans.
The required query is:
Q-20. Write an SQL query to print details of the Workers who have joined
in Feb’2014.
Ans.
The required query is:
Select * from Worker where year(JOINING_DATE) = 2014 and
month(JOINING_DATE) = 2;
Q-21. Write an SQL query to fetch the count of employees working in the
department ‘Admin’.
Ans.
The required query is:
Q-22. Write an SQL query to fetch worker names with salaries >= 50000
and <= 100000.
Ans.
The required query is:
FROM worker
WHERE WORKER_ID IN
Q-23. Write an SQL query to fetch the no. of workers for each department
in the descending order.
Ans.
The required query is:
FROM worker
GROUP BY DEPARTMENT
Ans.
The required query is:
FROM Worker W
ON W.WORKER_ID = T.WORKER_REF_ID
Q-25. Write an SQL query to fetch duplicate records having matching data
in some fields of a table.
Ans.
The required query is:
FROM Title
Q-26. Write an SQL query to show only odd rows from a table.
Ans.
The required query is:
Q-27. Write an SQL query to show only even rows from a table.
Ans.
The required query is:
SELECT * FROM Worker WHERE MOD (WORKER_ID, 2) = 0;
Q-28. Write an SQL query to clone a new table from another table.
Ans.
The general query to clone a table with data is:
Ans.
The required query is:
INTERSECT
Q-30. Write an SQL query to show records from one table that another
table does not have.
Ans.
The required query is:
MINUS
Ans.
Following MySQL query returns the current date:
SELECT CURDATE();
SELECT NOW();
Following SQL Server query returns the current date and time:
SELECT getdate();
Q-32. Write an SQL query to show the top n (say 10) records of a table.
Ans.
Following MySQL query will return the top n records using the LIMIT method:
Following SQL Server query will return the top n records using the TOP command:
Following Oracle query will return the top n records with the help of ROWNUM:
Q-33. Write an SQL query to determine the nth (say n=5) highest salary
from a table.
Ans.
The following MySQL query returns the nth highest salary:
SELECT Salary FROM Worker ORDER BY Salary DESC LIMIT n-1,1;
The following SQL Server query returns the nth highest salary:
FROM (
FROM Worker
Q-34. Write an SQL query to determine the 5th highest salary without
using TOP or limit method.
Ans.
The following query is using the correlated subquery to return the 5th highest salary:
SELECT Salary
FROM Worker W1
WHERE 4 = (
FROM Worker W2
);
Use the following generic method to find nth highest salary without using TOP or limit.
SELECT Salary
FROM Worker W1
WHERE n-1 = (
FROM Worker W2
);
Q-35. Write an SQL query to fetch the list of employees with the same
salary.
Ans.
The required query is:
Q-36. Write an SQL query to show the second highest salary from a table.
Ans.
The required query is:
Q-37. Write an SQL query to show one row twice in results from a table.
Ans.
The required query is:
select FIRST_NAME, DEPARTMENT from worker W where
W.DEPARTMENT='HR'
union all
Ans.
The required query is:
INTERSECT
Q-39. Write an SQL query to fetch the first 50% records from a table.
Ans.
The required query is:
SELECT *
FROM WORKER
Q-40. Write an SQL query to fetch the departments that have less than
five people in it.
Ans.
The required query is:
Ans.
The following query returns the expected result:
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:
Ans.
The required query is:
Q-44. Write an SQL query to fetch the last five records from a table.
Ans.
The required query is:
UNION
Ans.
The required query is:
and TempNew.TotalSalary=t.Salary;
Q-46. Write an SQL query to fetch three max salaries from a table.
Ans.
The required query is:
Q-47. Write an SQL query to fetch three min salaries from a table.
Ans.
The required query is:
Q-48. Write an SQL query to fetch nth max salaries from a table.
Ans.
The required query is:
SELECT distinct Salary from worker a WHERE n >= (SELECT
count(distinct Salary) from worker b WHERE a.Salary <=
b.Salary) order by a.Salary desc;
Q-49. Write an SQL query to fetch departments along with the total
salaries paid for each of them.
Ans.
The required query is:
Q-50. Write an SQL query to fetch the names of workers who earn the
highest salary.
Ans.
The required query is:
RESOURCES