Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

SQL Question

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

SQL Question

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Q-1.

Write an SQL query to fetch “FIRST_NAME” from Worker table using the alias name as
<WORKER_NAME>

SELECT FIRST_NAME AS WORKER_NAME


FROM Worker;

Q-2. Write an SQL query to fetch “FIRST_NAME” from Worker table in upper case.

SELECT UPPER(FIRST_NAME) AS UPPER_CASE_FIRST_NAME


FROM Worker;

Q-3. Write an SQL query to fetch unique values of DEPARTMENT from Worker table.

SELECT DISTINCT DEPARTMENT


FROM Worker;

Q-4. Write an SQL query to print the first three characters of FIRST_NAME from Worker table.

SELECT SUBSTRING(FIRST_NAME, 1, 3) AS FIRST_THREE_CHARACTERS


FROM Worker;

Q-5. Write an SQL query to find the position of the alphabet (‘a’) in the first name column
‘Amitabh’ from Worker table.

SELECT INSTR(FIRST_NAME, 'a') AS POSITION_OF_A


FROM Worker
WHERE FIRST_NAME = 'Amitabh';

Q-6. Write an SQL query to print the FIRST_NAME from Worker table after removing white spaces
from the right side.

SELECT RTRIM(FIRST_NAME) AS TRIMMED_FIRST_NAME


FROM Worker;

Q-7. Write an SQL query to print the DEPARTMENT from Worker table after removing white spaces
from the left side.

SELECT LTRIM(DEPARTMENT) AS TRIMMED_DEPARTMENT


FROM Worker;

Q-8. Write an SQL query that fetches the unique values of DEPARTMENT from Worker table and
prints its length.

SELECT DISTINCT DEPARTMENT, LENGTH(DEPARTMENT) AS DEPARTMENT_LENGTH


FROM Worker;

Q-9. Write an SQL query to print the FIRST_NAME from Worker table after replacing ‘a’ with ‘A’.

SELECT REPLACE(FIRST_NAME, 'a', 'A') AS REPLACED_FIRST_NAME


FROM Worker;
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.

SELECT CONCAT(FIRST_NAME, ' ', LAST_NAME) AS COMPLETE_NAME


FROM Worker;

Q-11. Write an SQL query to print all Worker details from the Worker table order by FIRST_NAME
Ascending.

SELECT *FROM Worker


ORDER BY FIRST_NAME ASC;

Q-12. Write an SQL query to print all Worker details from the Worker table order by FIRST_NAME
Ascending and DEPARTMENT Descending.

SELECT *FROM Worker


ORDER BY FIRST_NAME ASC, DEPARTMENT DESC;

Q-13. Write an SQL query to print details for Workers with the first name as “Vipul” and “Satish”
from Worker table.

SELECT *FROM Worker


WHERE FIRST_NAME IN ('Vipul', 'Satish');

Q-14. Write an SQL query to print details of workers excluding first names, “Vipul” and “Satish”
from Worker table.

SELECT *FROM Worker


WHERE FIRST_NAME NOT IN ('Vipul', 'Satish');

Q-15. Write an SQL query to print details of Workers with DEPARTMENT name as “Admin”.

SELECT *FROM Worker


WHERE DEPARTMENT = 'Admin';

Q-16. Write an SQL query to print details of the Workers whose FIRST_NAME contains ‘a’.

SELECT *FROM Worker


WHERE FIRST_NAME LIKE '%a%';

Q-17. Write an SQL query to print details of the Workers whose FIRST_NAME ends with ‘a’.
SELECT *

FROM Worker
WHERE FIRST_NAME LIKE '%a';

Q-18. Write an SQL query to print details of the Workers whose FIRST_NAME ends with ‘h’ and
contains six alphabets.

SELECT *FROM Worker


WHERE FIRST_NAME LIKE '____h' AND LENGTH(FIRST_NAME) = 6;
Q-19. Write an SQL query to print details of the Workers whose SALARY lies between 100000 and
500000.

SELECT *FROM Worker


WHERE SALARY BETWEEN 100000 AND 500000;

Q-20. Write an SQL query to print details of the Workers who have joined in Feb’2014.

SELECT *FROM Worker


WHERE MONTH(JOINING_DATE) = 2 AND YEAR(JOINING_DATE) = 2014;

Q-21. Write an SQL query to fetch the count of employees working in the department ‘Admin’.

SELECT COUNT(*) AS EmployeeCount


FROM Worker
WHERE DEPARTMENT = 'Admin';

Q-22. Write an SQL query to fetch worker names with salaries >= 50000 and <= 100000.

SELECT FIRST_NAME, LAST_NAME, SALARY


FROM Worker
WHERE SALARY BETWEEN 50000 AND 100000;

Q-23. Write an SQL query to fetch the no. of workers for each department in the descending order.

SELECT DEPARTMENT, COUNT(*) AS WorkerCount


FROM Worker
GROUP BY DEPARTMENT
ORDER BY WorkerCount DESC;

Q-24. Write an SQL query to print details of the Workers who are also Managers.

SELECT *
FROM Worker
WHERE IS_MANAGER = 1;

Q-25. Write an SQL query to fetch duplicate records having matching data in some fields of a table.

SELECT *
FROM your_table
WHERE (COLUMN1, COLUMN2, COLUMN3) IN (
SELECT COLUMN1, COLUMN2, COLUMN3
FROM your_table
GROUP BY COLUMN1, COLUMN2, COLUMN3
HAVING COUNT(*) > 1
);

Q-26. Write an SQL query to show only odd rows from a table.
SELECT * FROM your_table
WHERE MOD(your_primary_key_column, 2) <> 0;
Q-27. Write an SQL query to show only even rows from a table.

SELECT * FROM your_table


WHERE MOD(your_primary_key_column, 2) = 0;

Q-28. Write an SQL query to clone a new table from another table.

CREATE TABLE new_table AS


SELECT * FROM old_table;

Q-29. Write an SQL query to fetch intersecting records of two tables.

SELECT * FROM your_table1


INNER JOIN your_table2 ON your_table1.common_column = your_table2.common_column;

Q-30. Write an SQL query to show records from one table that another table does not have.

SELECT * FROM table1


WHERE NOT EXISTS (
SELECT 1
FROM table2
WHERE table1.common_column = table2.common_column
);

Q-31. Write an SQL query to show the current date and time.

SELECT CURRENT_TIMESTAMP AS CurrentDateTime;

Q-32. Write an SQL query to show the top n (say 10) records of a table.

SELECT *
FROM your_table
LIMIT 10;

Q-33. Write an SQL query to determine the nth (say n=5) highest salary from a table.

SELECT DISTINCT Salary


FROM your_table
ORDER BY Salary DESC
LIMIT 1 OFFSET 4;

Q-34. Write an SQL query to determine the 5th highest salary without using TOP or limit method.

SELECT DISTINCT Salary


FROM your_table a
WHERE 5 = (
SELECT COUNT(DISTINCT Salary)
FROM your_table b
WHERE a.Salary <= b.Salary
);
Q-35. Write an SQL query to fetch the list of employees with the same salary.

SELECT Salary, GROUP_CONCAT(EmployeeName) AS EmployeesWithSameSalary


FROM your_table
GROUP BY Salary
HAVING COUNT(*) > 1;

Q-36. Write an SQL query to show the second highest salary from a table.

SELECT DISTINCT Salary


FROM your_table
ORDER BY Salary DESC
LIMIT 1 OFFSET 1;

Q-37. Write an SQL query to show one row twice in results from a table.

SELECT *FROM your_table


UNION ALL
SELECT *
FROM your_table
LIMIT 1;

Q-38. Write an SQL query to fetch intersecting records of two tables.

SELECT *FROM your_table1


INNER JOIN your_table2 ON your_table1.common_column = your_table2.common_column;

Q-39. Write an SQL query to fetch the first 50% records from a table.

SELECT *FROM your_table


LIMIT ROUND(0.5 * (SELECT COUNT(*) FROM your_table));

Q-40. Write an SQL query to fetch the departments that have less than five people in it.

SELECT DEPARTMENT, COUNT(*) AS EmployeeCount


FROM your_table
GROUP BY DEPARTMENT
HAVING EmployeeCount < 5;

Q-41. Write an SQL query to show all departments along with the number of people in there.

SELECT DEPARTMENT, COUNT(*) AS EmployeeCount


FROM your_table
GROUP BY DEPARTMENT;

Q-42. Write an SQL query to show the last record from a table.

SELECT *
FROM your_table
ORDER BY your_primary_key_column DESC
LIMIT 1;
Q-43. Write an SQL query to fetch the first row of a table. B

SELECT * FROM your_table


LIMIT 1;

Q-44. Write an SQL query to fetch the last five records from a table.

SELECT *FROM your_table


ORDER BY your_primary_key_column DESC
LIMIT 5;

Q-45. Write an SQL query to print the name of employees having the highest salary in each
department.

WITH RankedSalaries AS (
SELECT
EmployeeName,
DEPARTMENT,
SALARY,
ROW_NUMBER() OVER (PARTITION BY DEPARTMENT ORDER BY SALARY DESC) AS SalaryRank
FROM your_table
)
SELECT EmployeeName, DEPARTMENT, SALARY
FROM RankedSalaries
WHERE SalaryRank = 1;

Q-46. Write an SQL query to fetch three max salaries from a table.

SELECT DISTINCT Salary


FROM your_table
ORDER BY Salary DESC
LIMIT 3;

Q-47. Write an SQL query to fetch three min salaries from a table.
SELECT DISTINCT Salary
FROM your_table
ORDER BY Salary
LIMIT 3;

Q-48. Write an SQL query to fetch nth max salaries from a table.

SELECT DISTINCT Salary


FROM your_table
ORDER BY Salary DESC
LIMIT 1 OFFSET n-1;
Q-49. Write an SQL query to fetch departments along with the total salaries paid for each of them.

SELECT DEPARTMENT, SUM(SALARY) AS TotalSalaries


FROM your_table
GROUP BY DEPARTMENT;

Q-50. Write an SQL query to fetch the names of workers who earn the highest salary.

SELECT WorkerName
FROM your_table
ORDER BY Salary DESC
LIMIT 1;

You might also like