Some Important SQL+DBMS
Some Important SQL+DBMS
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:
Select INSTR(FIRST_NAME, BINARY'a') from Worker where
FIRST_NAME = 'Amitabh';
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:
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:
Select * from Worker where FIRST_NAME not in
('Vipul','Satish');
Ans.
The required query is:
Ans.
The required query is:
Ans.
The required query is:
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:
FROM Worker W
ON W.WORKER_ID = T.WORKER_REF_ID
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:
SELECT * FROM Worker WHERE MOD (WORKER_ID, 2) <> 0;
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:
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
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:
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:
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:
SELECT t.DEPARTMENT,t.FIRST_NAME,t.Salary from(SELECT
max(Salary) as TotalSalary,DEPARTMENT from Worker group by
DEPARTMENT) as TempNew
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:
Q-49. Write an SQL query to fetch departments along with the total
salaries paid for each of them.
Ans.
The required query is:
SELECT DEPARTMENT, sum(Salary) from worker group by
DEPARTMENT;
Q-50. Write an SQL query to fetch the names of workers who earn the
highest salary.
Ans.
The required query is:
DBMS Theory
Database Management System (DBMS) is a software for storing and retrieving users' data
while considering appropriate security measures. Database Management System is a
software application used to access, create, and manage databases.
Advantages:
Data Integrity
Data Security
Better data integration
Minimized Data Inconsistency
Faster Data Access
2. What is Database?
A database is an organized collection of data, so that it can be easily accessed,
managed and updated.
A 3 Tier Architecture in DBMS is the most popular client server architecture
in DBMS in which the development and maintenance of functional processes,
logic, data access, data storage, and user interface is done independently as
separate modules. Three Tier architecture contains a presentation layer(client-
PC, mobile), an application layer(server), and a database server.
28. Difference between TRUNCATE and DELETE
S.NO Delete Truncate
1. The DELETE command is used While this command is used to delete all
to delete specified rows(one or the rows from a table.
more).
2. It is a DML(Data Manipulation While it is a DDL(Data Definition
Language) command. Language) command.
3. There may be WHERE clause in While there may not be WHERE clause
DELETE command in order to in TRUNCATE command.
filter the records.
4. In the DELETE command, a While in this command, data page is
tuple is locked before removing locked before removing the table data.
it.
5. The DELETE statement TRUNCATE TABLE removes the data
removes rows one at a time and by deallocating the data pages used to
records an entry in the store the table data and records only the
transaction log for each deleted page deallocations in the transaction
row. log.
6. DELETE command is slower While TRUNCATE command is faster
than TRUNCATE command. than DELETE command.
7. The delete can be used with Truncate cannot be used with indexed
indexed views. views.
30. Difference between share lock and exclusive lock, definition of lock.
A lock is a data variable which is associated with a data item. This lock
signifies what operations that can be performed on the data item. Locks in
DBMS help synchronize access to the database items by concurrent
transactions.
A shared lock is also called a Read-only lock. With the shared lock, the data
item can be shared between transactions. This is because you will never have
permission to update data on the data item.
With the Exclusive Lock, a data item can be read as well as written. This is
exclusive and can't be held concurrently on the same data item. Transactions
may unlock the data item after finishing the 'write' operation.