Prepare Sample Data To Practice SQL
Prepare Sample Data To Practice SQL
Prepare Sample Data To Practice SQL
WWW.PAVANONLINETRAININGS.COM
Sample Table – Bonus
WWW.PAVANONLINETRAININGS.COM
6 Lead 2016-06-11 00:00:00
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 Software’s and install them to carry on the SQL exercise.
WWW.PAVANONLINETRAININGS.COM
(003, 'Vishal', 'Singhal', 300000, '14-02-20 09.00.00', 'HR'),
(004, 'Amitabh', 'Singh', 500000, '14-02-20 09.00.00', 'Admin'),
(005, 'Vivek', 'Bhati', 500000, '14-06-11 09.00.00', 'Admin'),
(006, 'Vipul', 'Diwan', 200000, '14-06-11 09.00.00', 'Account'),
(007, 'Satish', 'Kumar', 75000, '14-01-20 09.00.00', 'Account'),
(008, 'Geetika', 'Chauhan', 90000, '14-04-11 09.00.00', 'Admin');
WWW.PAVANONLINETRAININGS.COM
ON DELETE CASCADE
);
Once above SQL would run, you’ll see a result similar to the one attached below.
WWW.PAVANONLINETRAININGS.COM
50 SQL Query Questions and Answers
Q-1. Write a SQL Query To Fetch “FIRST_NAME” From Worker Table Using The Alias Name As <WORKER_NAME>.
Ans.
The required query is:
Q-2. Write a SQL Query To Fetch “FIRST_NAME” From Worker Table In Upper Case.
Ans.
The required query is:
Q-3. Write a SQL Query To Fetch Unique Values Of DEPARTMENT From Worker Table.
Ans.
The required query is:
Q-4. Write a SQL Query To Print The First Three Characters Of FIRST_NAME From Worker Table.
Ans.
The required query is:
WWW.PAVANONLINETRAININGS.COM
Select substring(FIRST_NAME,1,3) from Worker;
Q-5. Write a SQL Query To Find The Position Of The Alphabet (‘A’) In The First Name Column ‘Amitabh’ From Worker
Table.
Ans.
The required query is:
Q-6. Write a SQL Query To Print The FIRST_NAME From Worker Table After Removing White Spaces From The Right
Side.
Ans.
The required query is:
Q-7. Write a SQL Query To Print The DEPARTMENT From Worker Table After Removing White Spaces From The Left
Side.
Ans.
The required query is:
WWW.PAVANONLINETRAININGS.COM
Q-8. Write a SQL Query That Fetches The Unique Values Of DEPARTMENT From Worker Table And Prints Its Length.
Ans.
The required query is:
Q-9. Write a SQL Query To Print The FIRST_NAME From Worker Table After Replacing ‘A’ With ‘A’.
Ans.
The required query is:
Q-10. Write a 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 a SQL Query To Print All Worker Details From The Worker Table Order By FIRST_NAME Ascending.
Ans.
The required query is:
WWW.PAVANONLINETRAININGS.COM
Select * from Worker order by FIRST_NAME asc;
Q-12. Write a 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 a SQL Query To Print Details For Workers With The First Name As “Vipul” And “Satish” From Worker Table.
Ans.
The required query is:
Q-14. Write a SQL Query To Print Details Of Workers Excluding First Names, “Vipul” And “Satish” From Worker Table.
Ans.
The required query is:
Q-15. Write a SQL Query To Print Details Of Workers With DEPARTMENT Name As “Admin”.
Ans.
WWW.PAVANONLINETRAININGS.COM
The required query is:
Q-16. Write a SQL Query To Print Details Of The Workers Whose FIRST_NAME Contains ‘A’.
Ans.
The required query is:
Q-17. Write a SQL Query To Print Details Of The Workers Whose FIRST_NAME Ends With ‘A’.
Ans.
The required query is:
Q-18. Write a SQL Query To Print Details Of The Workers Whose FIRST_NAME Ends With ‘H’ And Contains Six
Alphabets.
Ans.
The required query is:
Q-19. Write a SQL Query To Print Details Of The Workers Whose SALARY Lies Between 100000 And 500000.
WWW.PAVANONLINETRAININGS.COM
Ans.
The required query is:
Q-20. Write a SQL Query To Print Details Of The Workers Who Have Joined In Feb’2014.
Ans.
The required query is:
Q-21. Write a SQL Query To Fetch The Count Of Employees Working In The Department ‘Admin’.
Ans.
The required query is:
Q-22. Write a SQL Query To Fetch Worker Names With Salaries >= 50000 And <= 100000.
Ans.
The required query is:
WWW.PAVANONLINETRAININGS.COM
(SELECT WORKER_ID FROM worker
WHERE Salary BETWEEN 50000 AND 100000);
Q-23. Write a SQL Query To Fetch The No. Of Workers For Each Department In The Descending Order.
Ans.
The required query is:
Ans.
The required query is:
Q-25. Write a SQL Query To Fetch Duplicate Records Having Matching Data In Some Fields Of A Table.
Ans.
The required query is:
WWW.PAVANONLINETRAININGS.COM
HAVING COUNT(*) > 1;
Q-26. Write a SQL Query To Show Only Odd Rows From A Table.
Ans.
The required query is:
Q-27. Write a SQL Query To Show Only Even Rows From A Table.
Ans.
The required query is:
Ans.
The general query to clone a table with data is:
WWW.PAVANONLINETRAININGS.COM
Ans.
The required query is:
Q-30. Write a SQL Query To Show Records From One Table That Another Table Does Not Have.
Ans.
The required query is:
Q-31. Write a SQL Query To Show The Current Date And Time.
Ans.
Following MySQL query returns the current date:
SELECT CURDATE();
Following MySQL query returns the current date and time:
SELECT NOW();
Following SQL Server query returns the current date and time:
SELECT getdate();
WWW.PAVANONLINETRAININGS.COM
Following Oracle query returns the current date and time:
Q-32. Write a 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:
Q-33. Write a SQL Query To Determine The Nth (Say N=5) Highest Salary From A Table.
Ans.
The following MySQL query returns the nth highest salary:
WWW.PAVANONLINETRAININGS.COM
SELECT DISTINCT TOP n Salary
FROM Worker
ORDER BY Salary DESC
)
ORDER BY Salary ASC;
Q-34. Write a 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 = (
SELECT COUNT( DISTINCT ( W2.Salary ) )
FROM Worker W2
WHERE W2.Salary >= W1.Salary
);
Use the following generic method to find 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
);
WWW.PAVANONLINETRAININGS.COM
Q-35. Write a SQL Query To Fetch The List Of Employees With The Same Salary.
Ans.
The required query is:
Q-36. Write a SQL Query To Show The Second Highest Salary From A Table.
Ans.
The required query is:
Q-37. Write a SQL Query To Show One Row Twice In Results From A Table.
Ans.
The required query is:
WWW.PAVANONLINETRAININGS.COM
Ans.
The required query is:
Q-39. Write a SQL Query To Fetch The First 50% Records From A Table.
Ans.
The required query is:
SELECT *
FROM WORKER
WHERE WORKER_ID <= (SELECT count(WORKER_ID)/2 from Worker);
Q-40. Write a SQL Query To Fetch The Departments That Have Less Than Five People In It.
Ans.
The required query is:
Q-41. Write a SQL Query To Show All Departments Along With The Number Of People In There.
Ans.
The following query returns the expected result:
WWW.PAVANONLINETRAININGS.COM
SELECT DEPARTMENT, COUNT(DEPARTMENT) as 'Number of Workers' FROM Worker GROUP BY
DEPARTMENT;
Q-42. Write a 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 a SQL Query To Fetch The Last Five Records From A Table.
Ans.
The required query is:
WWW.PAVANONLINETRAININGS.COM
Q-45. Write a SQL Query To Print The Name Of Employees Having The Highest Salary In Each Department.
Ans.
The required query is:
Q-46. Write a SQL Query To Fetch Three Max Salaries From A Table.
Ans.
The required query is:
SELECT distinct Salary from worker a WHERE 3 >= (SELECT count(distinct Salary) from
worker b WHERE a.Salary <= b.Salary) order by a.Salary desc;
Q-47. Write a SQL Query To Fetch Three Min Salaries From A Table.
Ans.
The required query is:
SELECT distinct Salary from worker a WHERE 3 >= (SELECT count(distinct Salary) from
worker b WHERE a.Salary >= b.Salary) order by a.Salary desc;
Q-48. Write a SQL Query To Fetch Nth Max Salaries From A Table.
WWW.PAVANONLINETRAININGS.COM
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 a SQL Query To Fetch Departments Along With The Total Salaries Paid For Each Of Them.
Ans.
The required query is:
Q-50. Write a SQL Query To Fetch The Names Of Workers Who Earn The Highest Salary.
Ans.
The required query is:
SELECT FIRST_NAME, SALARY from Worker WHERE SALARY=(SELECT max(SALARY) from Worker);
WWW.PAVANONLINETRAININGS.COM