How To Print Duplicate Rows in A Table?: SELECT Name, Section FROM TBL GROUP BY Name, Section Having Count ( ) 1
How To Print Duplicate Rows in A Table?: SELECT Name, Section FROM TBL GROUP BY Name, Section Having Count ( ) 1
Ans: Database management systems were developed to handle the following difficulties of typical
Fille-processing systems supported by conventional operating systems.
1. Data redundancy and inconsistency
2. Difficulty in accessing data
3. Data isolation – multiple files and formats
4. Integrity problems
5. Atomicity of updates
6. Concurrent access by multiple users
7. Security problems
What are the differences between DDL, DML and DCL in SQL?
Ans: Following are some details of three.
DDL stands for Data Definition Language. SQL queries like CREATE, ALTER, DROP and
RENAME come under this.
DML stands for Data Manipulation Language. SQL queries like SELECT, INSERT and UPDATE
come under this.
DCL stands for Data Control Language. SQL queries like GRANT and REVOKE come under this.
o DELETE command delete only those rows which are specified with the WHERE
clause.
o DELETE command can be rolled back.
o DELETE command maintain a log, that's why it is slow.
o DELETE use row lock while performing DELETE function.
o The TRUNCATE command removes all the rows from the table.
o The TRUNCATE command cannot be rolled back.
o The TRUNCATE command doesn't maintain a log. That's why it is fast.
o TRUNCATE use table log while performing the TRUNCATE function.
Write an SQL query to fetch the names of workers who earn the highest
salary.
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:
Q-4. Write an SQL query to print the first three characters of FIRST_NAME
from Worker table.
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:
Q-7. Write an SQL query to print the DEPARTMENT from Worker table after
removing white spaces from the left side.
Ans.
The required query is:
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:
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:
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:
Q-14. Write an SQL query to print details of workers excluding first names,
“Vipul” and “Satish” from Worker table.
Ans.
The required query is:
Ans.
The required query is:
Ans.
The required query is:
Select * from Worker where FIRST_NAME like '%a%';
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:
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
Q-24. Write an SQL query to print details of the Workers who are also
Managers.
Ans.
The required query is:
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:
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
Q-31. Write an SQL query to show the current date and time.
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:
The following SQL Server query returns the nth highest salary:
FROM (
FROM Worker
)
ORDER BY Salary ASC;
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:
Select distinct W.WORKER_ID, W.FIRST_NAME, W.Salary
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:
union all
Ans.
The required query is:
INTERSECT
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:
Q-41. Write an SQL query to show all departments along with the number of
people in there.
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
Q-45. Write an SQL query to print the name of employees having the
highest salary in each department.
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:
Ans.
The required query is:
Q-48. Write an SQL query to fetch nth max salaries from a table.
Ans.
The required query is:
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: