SQL theory & Query
SQL theory & Query
DBMS RDMS
DBMS stores data as file RDBMS stores data in tabular form.
Is is software that is used to delete, create It is used to store and manage only the data that are
and maintain a database and provides in the form of tables
controlled access to data.
Normalization is not present. Normalization is present.
It is used for small organization and deal with It is used to handle large amount of data.
small data.
It supports single user. It supports multiple users.
Examples: XML, Window Registry, etc. Examples: MySQL, PostgreSQL, SQL Server, Oracle,
Microsoft Access etc.
Low software and hardware necessities. Higher software and hardware necessities.
12.What Is The Main Disadvantage Of Deleting Data From An Existing Table Using The DROP TABLE
Command?
⦁ DROP TABLE command deletes complete data from the table along with removing the complete table
structure too.
⦁ In case our requirement was only just remove the data, then we would need to recreate the table to store
data in it.
⦁ In such cases, it is advised to use the TRUNCATE command.
b. Non-unique indexes :
⦁ Non-unique indexes on the other hand, are not used to enforce constraints on the tables with which they
are associated.
⦁ Instead, non-unique indexes are used solely to improve query performance by maintaining a sorted order
of data values that are used frequently.
Filtered Index :
⦁ A filtered index is an optimized disk-based rowstore nonclustered index especially suited to cover
queries that select from a well-defined subset of data.
⦁ It uses a filter predicate to index a portion of rows in the table.
⦁ A correlated subquery : cannot be considered as an independent query, but it can refer to the column in a
table listed in the FROM of the main query.
⦁ A non-correlated subquery :can be considered as an independent query and the output of the subquery is
substituted in the main query.
23. What are some common clauses used with SELECT query in SQL?
1. WHERE clause it is used to filter records that are necessary, based on specific conditions.
2. ORDER BY clause it is used to sort the records based on some field(s) in ascending (ASC) or descending
order (DESC).
3. GROUP BY clause it is used to group records with identical data and can be used in conjunction with some
aggregation functions to produce summarized results from the database.
4. HAVING clause it is used to filter records in combination with the GROUP BY clause. It is different
from WHERE, since the WHERE clause cannot filter aggregated records.
WHERE , ORDER BY clause GROUP BY ,HAVING clause
SELECT * SELECT COUNT(studentId), country
FROM students FROM students
WHERE graduation_year = 2019 WHERE country != "INDIA"
ORDER BY studentID DESC; GROUP BY country
HAVING COUNT(studentID) > 5;
Minus Intersect
The MINUS operator is used to remove combines the result-set fetched by
duplicates from the result-set obtained by the the two SELECT statements ,
second SELECT query from the result-set where records from one match the
obtained by the first SELECT query . other and then returns this
then return the filtered results from the first. intersection of result-sets.
SELECT columns FROM table1 SELECT columns FROM table1
MINUS INTERSECT
SELECT columns FROM table2; SELECT columns FROM table2;
SELECT name FROM Students SELECT name FROM Students
MINUS INTERSECT
SELECT name FROM Contacts; SELECT name FROM Contacts;
40. What is the Diffrance between Aggregate function and windows function?
Aggregate Functions Window Functions
1. Atomicity:
⦁ It ensures that all statements or operations within the transaction unit must be executed
successfully.
⦁ Its main features are COMMIT, ROLLBACK, and AUTO-COMMIT.
2. Consistency:
⦁ This property ensures that the data must meet all validation rules.
⦁ In simple words, we can say that the database changes state only when a transaction will be committed
successfully.
⦁ It also protects data from crashes.
3. Isolation:
⦁ This property guarantees that the concurrent property of execution in the transaction unit must be
operated independently.
⦁ It also ensures that statements are transparent to each other.
⦁ The main goal of providing isolation is to control concurrency in a database.
4. Durability:
⦁ This property guarantees that once a transaction has been committed, it persists permanently
even if the system crashes, power loss, or failed.
SQL ( QUERY )
Q.1 Write a query to calculate 6th and Nth highest salary using SQL?
or find maximum salary ?
i. using windows function like rownumber to find 6th nad nth of salary :
select * from (
select salary , rownum r
from ( select distinct salary
from employee
order by salary desc))
where r = 6;
select * from (
select employee_id,First_name,salary,
rank() over(order by salary desc )r1, >>> when duplicate record then
not used
dense_rank() over(order by salary desc )r2 >>> when duplicate record
then used it.
from employee)
where r2 = 6;
4. using row limting clause : is availabe for only oracle version 12.1
# [note : (n-1) --> n = 1 then show 1st higest , n = 2 then show 2nd highest , n =
3 ... ]
select input from ( select input from table_A order by rownum desc)
where rownum <=3;
select input,r
from (
select input,row_number() over(order by rownum desc) r
from table_A)
where rownum <=3;
select input
from table_A
offset (select count(*) from table_A )-3 rows
fetch next 3 rows only ;
Q.4. Write a query to find First, last and middle record in a table?
First Record From table
SELECT TOP 1 FROM Employee;
SELECT * FROM Employee
WHERE Emp_ID = (SELECT MIN(Emp_ID) FROM Employee);
Last record From Table
SELECT * FROM Employee
WHERE Emp_ID = (SELECT MAX(Emp_ID) FROM Employee);
Middle record From Table
SELECT * FROM Employee
WHERE Emp_ID = ROUND ((SELECT MAX(Emp_ID) FROM Employee)/2);
Q.9.Write a query to calculate the even and odd records from a table?
For Even
SELECT * FROM Employee
WHERE MOD(Emp_ID,2) = 0;
For Odd
SELECT * FROM Employee
WHERE MOD(Emp_ID,2) = 1;
Q.10 How do you copy all the rows of a table using SQL query?
CREATE TABLE Emp_Detail AS SELECT * FROM Employee
Q.11 How do you copy schema of a table or create empty table from existing table?
CREATE TABLE Emp_Detail AS
SELECT * FROM Employee
WHERE 3 = 4;
Q.12 Write a query to retrieve the list of employees working in the same department?
SELECT DISTINCT E. Emp_ID, E. Emp_name, E. Department
FROM Employee E, Employee E1
WHERE E. Department = E1. Department
AND E. Emp_ID <> E1. Emp_ID;
Q.13 Write a query to retrieve first four character of Employee name from Employee Table?
SELECT SUBSTRING(E_name,1,4) FROM Employee;
Q.14 Write a query to find number of Employees Whose DOB is between 01/06/1995 to 30/06/1998
and are grouped according to gender?
SELECT COUNT (*), Gender
FROM Employee WHERE DOB BETWEEN ‘1995/06/01’ AND ‘1998/06/30’
GROUP BY gender;
Q.15 Write a query to fetch all records from Employee table order by department in Ascending order
and salary in the descending order?
SELECT * FROM Employee
ORDER BY Department ASC, salary DESC;
Q.16 Write a query to fetch details of all Employees excluding the employees who are ‘HR’ & ‘ADMIN’
SELECT * FROM Employee
WHERE Department
NOT IN (‘HR’,’ADMIN’);
Q.17 Write a query to fetch 50% records from the Employee table?
When ID is in Proper Sequence i.e no record deleted
Q.18 Write a query to display total salary of each Employee after adding 10% increment in the
salary?
SELECT Emp_id, E_name, salary+(salary/10)
AS Total_salary FROM Employee;
Q.20.Write a query to fetch the Employee name and replace the space with ‘_’ ?
SELECT REPLACE (E_name,’ ‘, ’_’ )
FROM Employee;
Q.21.Write a query to fetch the Employee gender and replace the ‘M’ with ‘Male’?
SELECT *, REPLACE (gender,’M‘,’Male’)
FROM Employee;
Q.22. Write a query to fetch details of Employees whose EmpLname ends with an alphabet ‘A’ and
contains five alphabets?
SELECT * FROM Employee
WHERE EmpLname LIKE ‘____a’;