Riza Washiq SQL
Riza Washiq SQL
DATA
DATABASE
Database is a place where we can store the data in a systematic & organized manner.
1. Create / Insert
2. Read / Retrieve
3. Update / Modify
4. Delete / Modify
• It is a type of DBMS software where we store the data in the form of tables.
• We use SQL (Structured Query Language) to communicate with RDBMS.
• RDBMS follows the theory of “Relational Model”.
Relational Model
It is a theory designed by a Data Scientist named E.F. Codd which says that data should be stored in
the form of tables.
2. In RDBMS, we store everything in the form of tables including meta data (The details about
the data are meta data).
3. According to E.F. Codd, we can store data in multiple tables. If needed, we can establish
connection between two tables using key attributes.
4. We can validate the data entered into the table in 2 steps,
a. By assigning datatypes.
b. By assigning constraints.
DATATYPES
Datatypes are used to determine what type or kind of data will be stored in particular memory
location.
Datatypes in SQL
1. CHAR
2. VARCHAR
3. NUMBER
4. DATE
5. LARGE OBJECT
a. Character Large Object (CLOB)
b. Binary Large Object (BLOB)
CHAR
It is a datatype in which we can store characters from A-Z, a-z, 0-9 & special characters (#, $, ...)
Syntax:
Page |3
VARCHAR
In VARCHAR, we can pass arguments like A-Z, a-z, 0-9 & special characters (#, $, ...)
Syntax:
VARCHAR2
NUMBER
Syntax:
Page |4
DATE
Syntax: DATE
LARGE OBJECT
CONSTRAINTS
Constraints are the conditions that are assigned on a particular column to validate the data.
Types of Constraints
1. UNIQUE
Page |5
2. NOT NULL
3. CHECK
4. PRIMARY KEY
5. FOREIGN KEY
UNIQUE
It is a constraint that is assigned to a particular column which cannot accept the repeated values.
NOT NULL
It is a constraint we assign to a particular column which cannot be a NULL or, records which are
mandatory.
CHECK
PRIMARY KEY
• It is a constraint we assign to a particular column to identify the records uniquely from the
table.
• Primary Key must be unique & it cannot be a NULL.
• It is better to have only one primary key in table.
FOREIGN KEY
ATTRIBUTES
An attribute which is used to identify a record uniquely from the table is called Key Attribute.
NON-KEY ATTRIBUTE
All the attributes except Key Attributes are referred as Non-Key Attributes.
Among the Key Attributes, an attribute is chosen to be the main attribute to identify the record
uniquely from the table.
Page |6
All the key attributes except Prime-Key Attribute is referred as Non-Prime Key Attribute.
It is a combination of two or more Non-Key Attributes, which is used to identify the record uniquely
from the table.
STATEMENTS IN SQL
SELECT
This statement is used to retrieve the data from database & display it.
Page |7
PROJECTION
• This statement is used to retrieve the data from the database by selecting only column.
• All the values in the column will be selected by default.
SELECTION
This statement is used to retrieve the data from database by selecting both columns as well as records.
JOINS
This statement is used to retrieve the data from multiple tables simultaneously.
NOTE:
FROM EMP;
NOTE:
If we want to display more than one column, we have to separate the columns by using comma (,).
FROM EMP;
NOTE:
We have to write the same column name or the table name i.e., present in the database.
FROM EMP;
or,
SELECT *
FROM EMP;
Page |9
SELECT *
FROM TAB;
FROM EMP;
Q5. WAQTD employee ID & department no. of all the employees in EMP table?
FROM EMP;
Q6. WAQTD employee name & hire date of all the employees?
FROM EMP;
FROM EMP;
Q8. WAQTD name & annual salary given to all the employees?
FROM EMP;
FROM EMP;
25
SELECT ENAME, SAL, SAL + *SAL
100
FROM EMP;
Q11. WAQTD name, salary & also salary with 12% deduction?
12
SELECT ENAME, SAL, SAL – 100*SAL
FROM EMP;
Syntax:
FROM TABLE_NAME;
Order of Execution:
1. FROM
2. SELECT
EXPRESSIONS
1. Operand
A. COLUMN_NAME
B. Literals (Direct Values)
a. Number literals
b. Character literals
c. Date literals
2. Operators
NOTE:
‘Character Literal’ & ‘Date literal’ should be enclosed within single quotes.
ALIAS
For Example:
NOTE:
To display all the details of the table along with other column of different attribute we use the
following,
For Example:
Q12. WAQTD all the details of an employee along with the annual salary?
FROM EMP;
DISTINCT
• To remove repeated values or, duplicated values in result table, we use ‘DISTINCT’ clause.
• For DISTINCT clause, we can pass COLUMN_NAME or, an expression as an argument.
• DISTINCT clause should be used as the first argument in the SELECT clause.
• We can pass multiple columns for DISTINCT clause.
• It removes the combination of duplicates from all the columns.
FROM EMP;
Q14. WAQTD the different designations that are present in the employee table?
FROM EMP;
Q15. WAQTD different Department number as well as salaries that are present in the table?
FROM EMP;
SELECTION
Syntax:
FROM TABLE_NAME
WHERE <FILTER_CONDITIONS>;
Order of Execution:
1. FROM
2. WHERE
3. SELECT
SELECT *
FROM EMP
WHERE JOB=’SALESMAN’;
SELECT ENAME
FROM EMP
Q18. WAQTD details of an employee who are earning more than Rs.3000?
SELECT *
FROM EMP
WHERE SAL>3000;
Q19. WAQTD all the details of an employee along with the annual salary if the annual salary is more
than Rs.14000?
FROM EMP
P a g e | 14
NOTE:
FROM EMP
WHERE ENAME=’SCOTT’;
SELECT ENAME
FROM EMP
WHERE JOB=’MANAGER’;
SELECT ENAME
FROM EMP
WHERE HIREDATE>=’01-JAN-1983’;
Q23. WAQTD details of an employee who are working as a ‘CLERK’ & earning less than 1000?
SELECT *
FROM EMP
SAL<1000;
P a g e | 15
OPERATORS
LOGICAL OPERATOR
Q24. WAQTD details of an employee working as a ‘MANAGER’ & earning less than Rs.1500?
SELECT *
FROM EMP
SAL<1500;
SELECT ENAME
FROM EMP
DEPTNO=30;
Q26. WAQTD details of an employee working as a ‘MANAGER’ or earning less than Rs.2000.
SELECT *
P a g e | 16
FROM EMP
WHERE JOB=’MANAGER’ OR
SAL<2000;
SELECT ENAME
FROM EMP
WHERE DEPTNO=10 OR
DEPTNO=20;
Q28. WAQTD all the details along with annual salary if employee is working as ‘SALESMAN’ &
earning less than Rs.1250 & annual salary more than Rs.14,000.
FROM EMP
SAL<1250 AND
SAL*12>14000;
SELECT *
FROM EMP
DEPTNO=30;
SELECT *
FROM EMP
SAL<3000;
Q31. WAQTD names of an employee working as a ‘CLERK’ & hired before 1981.
SELECT ENAME
FROM EMP
HIREDATE<=’31-DEC-1980’;
Q32. WAQTD details of an employee working as a ‘PRESIDENT’ or earning more than Rs.3000.
SELECT *
FROM EMP
WHERE JOB=’PRESIDENT’ OR
SAL>3000;
Q33. WAQTD names of an employee hired after 1982 into department no. 20.
SELECT ENAME
FROM EMP
DEPTNO=20;
Q34. WAQTD all the details of an employee along with the annual salary if employee working as a
’CLERK’ in department no. 20 & annual salary must be less than Rs.15000.
FROM EMP
DEPTNO=20 AND
SAL*12<15000;
SELECT *
FROM EMP
WHERE JOB=’PRESIDENT’ OR
JOB=’SALESMAN’ OR
P a g e | 18
JOB=’ANALYST’;
Q36. WAQTD names of an employee hired after 1981 & earning more than Rs.2000.
SELECT ENAME
FROM EMP
SAL>2000;
Q37. WAQTD details of an employee working as ‘ANALYST’ or working in department no. 10.
SELECT *
FROM EMP
WHERE JOB=’ANALYST’ OR
DEPTNO=10;
Q38. WAQTD names of an employee hired after 1981 & before 1987.
SELECT ENAME
FROM EMP
HIREDATE<=’31-DEC-1986’;
SELECT ENAME
FROM EMP
HIREDATE<=’31-DEC-1982’;
‘IN’ OPERATOR
‘IN’ operator is a multi-valued operator in which we can pass multiple values at RHS.
Syntax:
SELECT *
P a g e | 19
FROM EMP
SELECT ENAME
FROM EMP
‘NOT IN’ operator is similar to ‘IN’ operator but it rejects the value instead of selecting it.
Syntax:
Q42. WAQTD details of an employee excluding the employees working in department no. 10 or 20.
SELECT *
FROM EMP
SELECT *
FROM EMP
‘BETWEEN’ OPERATOR
Syntax:
SELECT *
FROM EMP
Q45. WAQTD name & hire date of an employee hired after 1981 & before 1987.
FROM EMP
Q46. WAQTD details of an employee earning more than Rs.200 and hired after 1982.
SELECT *
FROM EMP
HIREDATE>=’01-JAN-1983’;
‘LIKE’ OPERATOR
Syntax:
1. PERCENTILE (%): It can accept any character any number of times or, no character.
2. UNDERSCORE ( _ ): It can accept any character but only once.
SELECT *
FROM EMP
SELECT *
FROM EMP
SELECT *
FROM EMP
Q50. WAQTD names of an employee whose name does not start with ‘S’.
SELECT ENAME
FROM EMP
Q51. WAQTD employee name if the employee having character ‘A’ in the second place.
SELECT ENAME
FROM EMP
SELECT *
FROM EMP
SELECT *
FROM EMP
SELECT ENAME
FROM EMP
Q55. WAQTD names of an employee having character ‘A’ in first place & ‘S’ in the last place.
SELECT ENAME
FROM EMP
Q56. WAQTD name, salary if they were hired in the year 82.
FROM EMP
FROM EMP
Q58. WAQTD details of an employee who are having character ‘A’ in first place or employees if they
are having character ‘S’ in the last place.
SELECT *
P a g e | 25
FROM EMP
Q59. WAQTD names of an employee if they were having ‘A’ in the first place & working in
department no. 10 or 20.
SELECT ENAME
FROM EMP
DEPTNO IN (10,20);
FROM EMP
Q61. WAQTD names of an employee, hire date if they are hired in the year 81 & earning salary more
than Rs.2000.
FROM EMP
SAL>2000;
Q62. WAQTD details of an employee if they having string ‘MAN’ in their job.
SELECT *
FROM EMP
Q63. WAQTD details of an employee if they are having ‘A’ in first place, ‘D’ in second place & ‘S’ in
last place in their names.
SELECT *
FROM EMP
Q64. WAQTD names of an employee working in department no. 30 or 40 & earning more than 2000.
SELECT ENAME
FROM EMP
SAL >2000;
SELECT *
FROM EMP
DEPTNO=10;
SELECT *
FROM EMP
SELECT *
FROM EMP
DEPTNO=20 AND
HIREDATE>=’01-JAN-1984’;
Q68. WAQTD all details of an employee along with annual salary if annual salary is more than 14000
& working as ‘SALESMAN’ or ‘ANALYST’ in department no. 20 or 30 & hired in the year 1982.
FROM EMP
Q69. WAQTD details of an employee working in department no. 20 or 30 & hired after 1981 before
1988.
SELECT *
FROM EMP
SELECT ENAME
FROM EMP
HIREDATE>=’01-JAN-1982’ AND
Q71. WAQTD all the details of an employee along with annual salary if the annual salary is more
than Rs.15000 in department 10 or 20 except employees hired in the year 1982.
FROM EMP
Q72. WAQTD all the detail of an employee hired after 1980 before 1988 except the employees hired
in the year 1982.
SELECT *
FROM EMP
HIREDATE NOT BETWEEN ’01-JAN-1982’ AND ’31-DEC-1982’; [or, HIREDATE NOT LIKE ‘%82’;]
‘IS’ OPERATOR
Syntax:
SELECT ENAME
FROM EMP
SELECT ENAME
FROM EMP
SELECT ENAME
FROM EMP
Q76. WAQTD names of an employee working as a ‘’PRESIDENT or ‘MANAGER’ except the employees
working in department no. 30 & hired after 1980 before 1987 except the employees hired in the
year 1982 & employees must be earning more than Rs.3000 & name has second character as ‘I’ &
he does not get any commission.
SELECT ENAME
FROM EMP
SAL>3000 AND
COMM IS NULL;
SELECT ENAME
FROM EMP
COMM=1400 AND
P a g e | 30
FUNCTIONS
Function
Single-row Multi-row
Function Function
SINGLE-ROW FUNCTION
• DUAL: It is a dummy table that is present in all the database to perform any operations.
• LOWER (): This function is used to convert the string from uppercase to lowercase.
• UPPER (): This function is used to convert the string from lowercase to uppercase.
• INITCAP (): This function is used to convert the initial characters into uppercase.
• LENGTH (): This function is used to find the number of characters in a given string.
• SUBSTR (): This function is used to extract the part of the string from the given original string.
Example: -9 -8 -7 -6 -5 -4 -3 -2 -1
1 2 3 4 5 6 7 8 9
B E N G A L U R U
SUBSTR (‘BENGALURU’, 3, 2) // NG
SUBSTR (‘BENGALURU’, 1, 1) // B
SUBSTR (‘BENGALURU’, 8) // RU
FROM DUAL;
• SYSTIMESTAMP: This function is used to find the date & time along with the time zone.
FROM DUAL;
• TO_CHAR(): This function is used to convert the given date to string format.
FORMAT TOOLS
1. YEAR
2. YYYY
3. YY
4. MONTH
5. MON
6. MM
7. DAY
8. DY
9. DD
10. D
11. HH24
12. HH12
13. MI
14. SS
• INSTR(): This function is used to obtain index value of the substring which is present in the
original string. (Index value- position of the character)
Syntax:
** Here, > 0 (is a condition if occurrence is greater than 0) & = 0 (is a condition if occurrence is equal
to 0).
• MOD (): It is a function which is used to find the modulus of a given number.
Example:
P a g e | 34
• REPLACE (): This function is used to replace the substring with new string in given original
string.
Syntax: REPLACE (‘ORIGINAL_STRING’, ‘SUB_STR’, [‘NEW_STR’])
MULTI-ROW FUNCTION
** In interview, interviewer never use the name multi-row function, they will simply ask what is
Group Function.
Note:
• Multi-row functions can accept only a single argument that is a COLUMN_NAME or, an
EXPRESSION.
• MAX () & MIN () functions can be used for all the following datatypes problems. i.e., CHAR,
VARCHAR, NUMBER & DATE.
• SUM () & AVG () functions can only take NUMBER COLUMN as an argument.
• Multi-row functions will ignore the NULL value.
• We can’t use multi-row functions in WHERE clause.
• We can’t use any COLUMN_NAME with multi-row functions in SELECT clause.
• COUNT () is the only multi-row function to which we can pass Asterisk (*) as an argument.
• MAX ()
• MIN ()
• SUM ()
• AVG ()
P a g e | 35
• COUNT ()
FROM EMP;
FROM EMP
WHERE DEPTNO=10;
FROM EMP;
FROM EMP
DEPTNO=20;
P a g e | 36
FROM EMP
WHERE SAL>3000;
FROM EMP
FROM EMP
FROM EMP
FROM EMP
FROM EMP
Q88. WAQTD maximum salary given to the employee hired in the month of February.
FROM EMP
Q89. WAQTD total salary given to an employee who is having two consecutive ‘T’ in their name.
FROM EMP
Q90. WAQTD total salary given to the employees whose name ends with character ‘N’.
FROM EM
Q91. WAQTD number of employees whose name starts with character ‘A’.
FROM EMP
Q92. WAQTD number of employees earning more than Rs.3000 in Department number 10 &
FROM EMP
DEPTNO=10 AND
‘CONCATENATION’ OPERATOR
FROM EMP;
SELECT ‘MR.’||ENAME||’ YOUR SAL IS RS.’||SAL||’ AND YOUR ANNUAL SALARY IS RS.’||SAL*12
FROM EMP;
• For ‘GROUP BY’ clause, we can pass COLUMN_NAME or, an EXPRESSION as an argument.
• We can write ‘GROUP BY’ expression along with the multi-row function in ‘SELECT’ clause.
• Any COLUMN_NAME or, an EXPRESSION that is written in ‘GROUP BY’ clause is known as
‘GROUP BY’ expression.
• After the execution of ‘GROUP BY’ clause, it creates group & anything that executes after
‘GROUP BY’ clause executes ‘GROUP-BY-GROUP’.
Syntax:
FROM TABLE_NAME
[WHERE <filter-condition>]
Order of Execution:
1. FROM
2. WHERE (if used) [row-by-row]
3. GROUP BY [row-by-row]
4. SELECT [group-by-group]
FROM EMP
GROUP BY DEPTNO;
FROM EMP
GROUP BY JOB;
P a g e | 40
Q96. WAQTD number of employees in each Department if the employees are earning more than
Rs.2000.
FROM EMP
WHERE SAL>2000
GROUP BY DEPTNO;
FROM EMP
GROUP BY DEPTNO;
FROM EMP
GROUP BY DEPTNO;
Q99. WAQTD average salary needed to pay to all the employees excluding the employees working
in department number 20 in each job.
P a g e | 41
FROM EMP
GROUP BY JOB;
Q100. WAQTD number of employees having character ‘A’ in their name in each job.
FROM EMP
GROUP BY JOB;
‘HAVING’ CLAUSE
Q101. WAQTD number of employees working in each department if there are at least 2 employees
in each department.
FROM EMP
GROUP BY DEPTNO
Q102. WAQTD total salary needed to pay all the employees in each job.
FROM EMP
P a g e | 42
GROUP BY JOB;
Q103. WAQTD number of employees & average salary needed to pay the employees whose salary
is greater than Rs.2000 in each department.
FROM EMP
WHERE SAL>2000
GROUP BY DEPTNO;
Q104. WAQTD number of employees & total salary given to all the salesman in each department.
FROM EMP
GROUP BY DEPTNO;
Q105. WAQTD number of employees with their maximum salaries in each job.
FROM EMP
GROUP BY JOB;
FROM EMP
GROUP BY DEPTNO;
FROM EMP
GROUP BY SAL;
Q108. WAQTD the number & total salary needed to pay all employees in each department if there
are at least 4 employees in each department.
FROM EMP
GROUP BY DEPTNO
Q109. WAQTD number of employees earning salary more than Rs.1200 in each job & the total salary
needed to pay employee of each job must exceed Rs.3800
P a g e | 43
FROM EMP
WHERE SAL>1200
GROUP BY JOB
Q110. WAQTD DEPTNO & number of employees working only if there are 2 employees working in
each department as Manager.
FROM EMP
GROUP BY DEPTNO
SELECT SAL
FROM EMP
GROUP BY SAL
Q112. WAQTD the hire date which are repeated in EMP table.
P a g e | 44
SELECT HIREDATE
FROM EMP
GROUP BY HIREDATE
Syntax:
FROM TABLE_NAME
[WHERE <filter-condition>]
[HAVING <group_filter_condition>]
Order of Execution:
1. FROM
2. WHERE [if used] (row-by-row)
3. GROUP BY [if used] (row-by-row)
4. HAVING [if used] (group-by-group)
5. SELECT (group-by-group)
6. ORDER BY
SELECT SAL
P a g e | 45
FROM EMP
FROM EMP
SUB-QUERY
Working Procedure
TYPES OF SUB-QUERY
1. Single-row Sub-query
2. Multi-row Sub-query
SINGLE-ROW SUB-QUERY
If, inner query returns exactly one output to the outer query, we call it as Single-row Sub-query.
MULTI-ROW SUB-QUERY
• If, inner query returns more than one output to the outer query, we call it as Muti-row Sub-
query.
• We can achieve multi-row sub-query by using ‘ALL’ & ‘ANY’ operator.
P a g e | 47
Q115. WAQTD names of an employee who are earning less than ‘ADAMS’.
SELECT ENAME
FROM EMP
FROM EMP
Q116. WAQTD details of an employee who are working in same department as that of ‘KING’.
SELECT *
FROM EMP
FROM DEPT
Q117. WAQTD details of an employee who are working in same designation as that of ‘SCOTT’.
SELECT *
FROM EMP
FROM EMP
Q118. WAQTD name & hire date of an employee hired after ‘FORD.’
P a g e | 48
FROM EMP
FROM EMP
Q119. WAQTD details of an employee earning more than ‘JONES’ & less than ‘KING’.
SELECT *
FROM EMP
FROM EMP
FROM EMP
Q120. WAQTD names of an employee who are working as a ‘MANAGER’ & earning more than
‘CLARK’.
SELECT ENAME
FROM EMP
FROM EMP
SELECT *
FROM EMP
FROM EMP
Q122. WAQTD no. of employee working as ‘PRESIDENT’ in same department of that of ‘KING’.
FROM EMP
FROM EMP
Q123. WAQTD maximum salary given to the employee working as ‘CLERK’ in the same department
as that of ‘SMITH’.
FROM EMP
FROM EMP
Q124. WAQTD name of an employee earning more than ‘JONES’ but less than ‘KING’.
SELECT ENAME
FROM EMP
FROM EMP
FROM EMP
Q125. WAQTD no. of employees hired in the month of ‘APRIL’ in the same department as that of
‘SCOTT’.
FROM EMP
FROM EMP
SELECT ENAME
FROM EMP
FROM EMP
SELECT ENAME
FROM EMP
FROM EMP
FROM EMP
Q128. WAQTD name of an employee working as a ‘MANAGER’ in same department as that of ‘KING’
& earning more than ‘MILLER’.
SELECT ENAME
FROM EMP
FROM EMP
FROM EMP
Q129. WAQTD no. of employees working as a ‘CLERK’ in same department as that of ‘ADAMS’ &
hired in the year 1980.
FROM EMP
FROM EMP
SELECT ENAME
FROM EMP
FROM EMP
Q131. WAQTD ENAME, SAL of the employee earning less than ‘KING’.
FROM EMP
FROM EMP
Q132. WAQTD ENAME, DEPT of the employees if they are working in the same department as
‘JONES’.
FROM EMP
FROM EMP
Q133. WAQTD ENAME, JOB of all the employees working in the same designation as ‘JAMES’.
FROM EMP
FROM EMP
Q134. WAQTD EMPNO, ENAME along with ANNUALSAL of all the employees if their ANNUALSAL is
greater than ‘WARD’ annual salary.
FROM EMP
P a g e | 52
FROM EMP
Q135. WAQTD ENAME, HIREDATE of the employees if they are hired before ‘TURNER’.
FROM EMP
FROM EMP
Q136. WAQTD ENAME, HIREDATE of the employees if they are hired after the ‘PRESIDENT’.
FROM EMP
FROM EMP
Q137. WAQTD ENAME & SAL of the employees if they are earning SAL less than the employee whose
EMPNO is 7839.
FROM EMP
FROM EMP
Q138. WAQTD all the details of an employee if the employees are hired before ‘MILLER’.
SELECT *
FROM EMP
FROM EMP
Q139. WAQTD ENAME & EMPNO of the employees if employees are earning more than ‘ALLEN’.
FROM EMP
FROM EMP
FROM EMP
FROM EMP
Q141. WAQTD total salary given to the employees working in the same department as of ‘WARD’.
FROM EMP
FROM EMP
Q142. WAQTD ENAME & SAL of the employees who are earning more than ‘MILLER’ but less than
‘ALLEN’.
FROM EMP
FROM EMP
FROM EMP
Q143. WAQTD ENAME & SAL of the employees who are earning more than Rs.1000 but less than
Rs.3000.
FROM EMP
Q144. WAQTD all the detail of the employees working in department 20 & working in the same
designation as ‘SMITH’.
SELECT *
FROM EMP
FROM EMP
FROM EMP
Q145. WAQTD all the details of an employee working as ‘MANAGER’ in the same department as
that of ‘TURNER’.
SELECT *
FROM EMP
FROM EMP
Q146. WAQTD ENAME & HIREDATE of an employees hired after 1980 & before ‘KING’.
FROM EMP
P a g e | 55
FROM EMP
Q147. WAQTD ENAME & SAL along with annual salary for all employees whose SAL is less than
‘BLAKE’ or employees earning more than 3500.
FROM EMP
FROM EMP
Q148. WAQTD all the details of employees who earn more than ‘SCOTT’ but less than ‘KING’.
SELECT *
FROM EMP
FROM EMP
FROM EMP
Q149. WAQTD ENAME of the employees whose name starts with ‘A’ & works in the same
department as ‘BLAKE’.
SELECT ENAME
FROM EMP
FROM EMP
Q150. WAQTD ENAME & COMM if employees earn commission & work in the same designation as
‘SMITH’.
FROM EMP
FROM EMP
Q151. WAQTD details of all the employees working as ‘CLERK’ in the same department as ‘TURNER’.
SELECT *
FROM EMP
FROM EMP
Q152. WAQTD ENAME, SAL & JOB of the employees whose annual salary is more than ‘SMITH’ &
less than ‘KING’.
FROM EMP
FROM EMP
FROM EMP
P a g e | 57
Case 2. Whenever the data to be found & condition to be executed are present in different tables, we
go for sub-queries.
P a g e | 58
SELECT DNAME
FROM DEPT
FROM EMP
SELECT LOC
FROM DEPT
FROM EMP
SELECT ENAME
FROM EMP
FROM DEPT
FROM EMP
FROM DEPT
Q136. WAQTD location of an employee whose name ends with character ‘E’.
SELECT LOC
FROM DEPT
FROM EMP
Q137. WAQTD names of an employee earning more than ‘MILLER’ in ‘ACCOUNTING’ department.
P a g e | 59
SELECT ENAME
FROM EMP
FROM EMP
FROM DEPT
SELECT ENAME
FROM EMP
FROM DEPT
Q139. WAQTD name of an employee working in same designation as that of ‘CLARK’ in ‘CHICAGO’.
SELECT ENAME
FROM EMP
FROM DEPT
FROM EMP
Q140. WAQTD no. of employees working in same designation as that of ‘KING’ in ‘NEW YORK’ &
hired in the year 1981.
FROM EMP
FROM DEPT
P a g e | 60
FROM EMP
SELECT ENAME
FROM EMP
FROM DEPT
FROM EMP
SELECT ENAME
FROM EMP
FROM EMP);
SELECT ENAME
FROM EMP
FROM EMP);
P a g e | 61
SELECT ENAME
FROM EMP
FROM EMP);
NESTED SUB-QUERY
SELECT ENAME
FROM EMP
FROM EMP
FROM EMP));
SELECT ENAME
P a g e | 62
FROM EMP
FROM EMP
FROM EMP
FROM EMP)));
SELECT LOC
FROM DEPT
FROM EMP
FROM EMP
FROM EMP)));
P a g e | 63
SELECT ENAME
FROM EMP
FROM EMP
FROM EMP
FROM EMP
FROM EMP))));
SELECT ENAME
FROM EMP
FROM EMP
FROM EMP
FROM EMP)));
Q150. WAQTD details of an employee who are earning more than all the ‘SALESMAN’.
SELECT *
FROM EMP
FROM EMP
Q151. WAQTD name & salary of an employee if they are earning more than at least a ‘MANAGER’.
FROM EMP
FROM EMP
Q152. WAQTD names of an employee hired after all the ‘MANAGER’ & earning more than all the
‘CLERK’.
SELECT ENAME
FROM EMP
FROM EMP
FROM EMP
SELECT DNAME
FROM DEPT
FROM EMP
WHERE ENAME='SMITH');
Q154. WAQTD DNAME & LOC of the employee whose ENAME is ‘KING’.
FROM DEPT
FROM EMP
WHERE ENAME='KING');
SELECT LOC
FROM DEPT
FROM EMP
Q156. WAQTD DNAME & LOC along with DEPTNO of the employee whose name ends with ‘R’.
FROM DEPT
FROM EMP
SELECT DNAME
FROM DEPT
FROM EMP
SELECT ENAME
P a g e | 66
FROM EMP
FROM DEPT
Q159. WAQTD ENAME & salaries of the employee who are working in the location ‘CHICAGO’.
FROM EMP
FROM DEPT
SELECT *
FROM EMP
FROM DEPT
Q161. WAQTD details of the employee along with annual salary if employees are working in ‘NEW
YORK’.
FROM EMP
FROM DEPT
SELECT ENAME
FROM EMP
FROM DEPT
Q163. WAQTD names of the employees earning more than ‘SCOTT’ in ‘ACCOUNTING’ department.
SELECT ENAME
P a g e | 67
FROM EMP
FROM EMP
FROM DEPT
Q164. WAQTD details of the employees working as the ‘MANAGER’ in the location ‘CHICAGO’.
SELECT *
FROM EMP
FROM DEPT
Q165. WAQTD ENAME & SAL of the employees earning more than ‘KING’ in the department
‘ACCOUNTING’.
FROM EMP
FROM EMP
FROM DEPT
Q166. WAQTD details of the employees working as ‘SALESMAN’ in the department ‘SALES’.
SELECT *
FROM EMP
FROM DEPT
Q167. WAQTD ENAME, SAL, JOB, HIREDATE of the employees working in ‘OPERATIONS’ department
& hired before ‘KING’.
FROM EMP
FROM DEPT
FROM EMP
Q168. WAQTD display all the details of an employee whose department name ending ‘S’.
SELECT *
FROM EMP
FROM DEPT
Q169. WAQTD DNAME of the employees whose name has character ‘A’ in it.
SELECT DNAME
FROM DEPT
FROM EMP
Q170. WAQTD DNAME & LOC of the employees whose salary is Rs.800.
FROM DEPT
FROM EMP
SELECT DNAME
FROM DEPT
P a g e | 69
FROM EMP
Q172. WAQTD LOC of the employees if they earn commission in department 40.
SELECT LOC
FROM DEPT
FROM EMP
DEPTNO = 40;
Q173. WAQTD details of the employees hired after all the ‘CLERKS’.
SELECT *
FROM EMP
FROM EMP
Q174. WAQTD ENAME & HIREDATE of employees hired before all the ‘MANAGER’S.
FROM EMP
FROM EMP
Q175. WAQTD details of the employees working as ‘CLERK’ & hired before at least a ‘SALESMAN’.
SELECT *
FROM EMP
FROM EMP
SELECT ENAME
FROM EMP
FROM EMP
SELECT ENAME
FROM EMP
FROM EMP
SELECT SAL
FROM EMP
FROM EMP
SELECT ENAME
FROM EMP
FROM EMP
FROM EMP
P a g e | 71
SELECT LOC
FROM DEPT
FROM EMP
FROM EMP
SELECT DNAME
FROM DEPT
FROM EMP
FROM EMP
FROM EMP
SELECT ENAME
FROM EMP
FROM EMP
FROM EMP
FROM EMP
SELECT *
FROM EMP
FROM EMP
SELECT ENAME
FROM EMP
FROM EMP
Q186. WAQTD names of an employee who are earning more than ‘ADAMS’ manager.
SELECT ENAME
FROM EMP
FROM EMP
FROM EMP
SELECT DNAME
FROM DEPT
FROM EMP
FROM EMP
FROM EMP
FROM EMP
FROM EMP
JOINS
TYPES OF JOINS
CARTESIAN JOIN
• In cartesian join, the records from table 1 will merge with all the records of table 2.
• Number of Columns in resultant table will be the summation of Column 1 & Column 2.
• Number of records in resultant table will be the product of records present in Table 1 & Table
2.
• In cartesian join, we will be getting error records.
Syntax:
P a g e | 75
INNER JOIN
• We use inner joins to obtain the matched records or, the records which are in pair.
• We use join condition to obtain the matched records.
Join Condition: It is a condition in which we merge two tables to get the matched records.
Syntax:
Q192. WAQTD ENAME, DEPTNO & DNAME of employees working in DEPTNO 20.
DEPTNO = 20;
Q193. WAQTD ENAME, DNAME of employees who are earning less than Rs.2000.
LOC = ‘DALLAS’;
Q195. WAQTD ENAME, SAL & DNAME of employees whose name starts with character ‘A’ & DNAME
ends with character ‘S’.
Q196. WAQTD ENAME, SAL, DNAME of all the employees who are earning more than ‘SCOTT’ in
‘ACCOUNTING’ department.
FROM EMP
DEPT = ‘ACCOUNTING’;
Q197. WAQTD number of employees hired before ‘ALLEN’ in ‘RESEARCH’ department using joins.
FROM EMP
DEPT = ‘RESEARCH’;
Q198. WAQTD maximum SAL given to the employees working in same designation as that of ‘BLAKE’
in ‘DALLAS’.
FROM EMP
LOC = ‘DALLAS’;
Q199. WAQTD ENAME, HIREDATE & DNAME of employees hired in the month of ‘FEB’ & his DNAME
must have second character as ‘A’.
Q200. WAQTD ENAME earning more than ‘MILLER’ in ‘NEW YORK’ using both Sub-query & joins.
Sub-Query Method:
SELECT ENAME
FROM EMP
FROM EMP
FROM DEPT
Join Method:
SELECT ENAME
FROM EMP
Q201. WAQTD ENAME & DNAME of employee who is having exactly 4 characters in his name & his
DNAME should have 2 consecutive 'CC'.
ENAME LIKE ‘_ _ _ _’
Q202. WAQTD DNAME, ENAME & LOC of employees hired before 1981 in ‘DALLAS’.
LOC = ‘DALLAS’;
Q203. WAQTD ENAME, JOB, LOC of employees working in same designation as that of ‘JONES’ in
‘CHICAGO’.
FROM EMP
LOC = ‘CHICAGO’;
Q204. WAQTD ENAME, LOC of employees who is searching same SAL as that of ‘SCOTT‘ in ‘DALLAS’
& he was hired in the month of ‘DEC’.
FROM EMP
Q205. WAQTD ENAME, DNAME of employees whose designation ends with string ‘MAN’ & his name
must have 2 consecutive ‘LL’.
Q206. WAQTD ENAME, DEPTNO & DNAME of employees working in same designation as that of
‘SMITH’ in ‘NEW YORK’.
FROM EMP
Q207. WAQTD ENAME & DNAME of employees hired after 1980 into ‘RESEARCH’ department &
working as an ‘ANALYST’.
JOB = ‘ANALYST’;
Q208. WAQTD ENAME, SAL, LOC of employees whose SAL ends with 50 in ‘CHICAGO’ & his name
must start with ‘M’ & ends with ‘N’.
Q209. WAQTD ENAME, DNAME of employees earning less than ‘JAMES’ & must be hired in the year
1980 in ‘DALLAS’.
FROM EMP
LOC = ‘DALLAS’;
Q210. WAQTD ENAME & DEPTNO & DNAME of employees working as ‘SALESMAN’ or ‘MANAGER’
in DEPTNO 20 or 30 & he must be earning more than ‘WARD’ in ‘SALES’ or ‘RESEARCH’ department
& he must get commission.
FROM EMP
Q211. WAQTD ENAME of the employees & their LOC of all the employees.
Q212. WAQTD DNAME & SAL for all the employee working in ‘ACCOUNTING‘ department.
DNAME = ‘ACCOUNTING’;
P a g e | 82
Q213. WAQTD DNAME & ANNUALSAL for all employees whose SAL is more than Rs.2340.
Q214. WAQTD ENAME & DNAME for employees having character ‘A’ in their DNAME.
Q215. WAQTD ENAME & DNAME for all the employees working as ‘SALESMAN’.
JOB IN (‘SALESMAN’);
Q216. WAQTD DNAME & JOB for all the employees whose JOB & DNAME starts with character ‘S’.
MGR = 7839;
Q218. WAQTD DNAME & HIREDATE for employees hired after 83 into ‘ACCOUNTING’ or ‘RESEARCH’
department.
Q219. WAQTD ENAME & DNAME of the employees who are getting COMM in DEPTNO 10 or 30.
Q220. WAQTD DNAME & EMPNO for all the employees whose EMPNO are (7839, 7902) & are
working in LOC = ‘NEW YORK’.
Q221. WAQTD ENAME & DNAME who are earning more than ‘SMITH’.
FROM EMP
SELF-JOIN
It is used to join the same two tables or, the table itself.
If the data to be selected & condition to be executed are present in same table but different records
we go for self-join.
P a g e | 84
Syntax:
Q223. WAQTD employees name & managers name of if employee is working in DEPTNO 20.
E1.DEPTNO = 20;
E2.JOB = ‘PRESIDENT’;
Q225. WAQTD employee name, employee SAL, manager name, manager SAL if employee is earning
less than 1000.
Q226. WAQTD employee name, employee HIREDATE, manager’s name, manager’s HIREDATE if
employees is hired after 1980 & manager hired before 1987.
Q227. WAQTD name of the employee & his manager’s name if employee is working as ‘CLERK’.
E1.JOB = ‘CLERK’;
E2.DEPTNO IN (10,20);
Q229. WAQTD employee name & manager name if employee is hired before 1982.
Q230. WAQTD employee name, manager’s name if employee & manager both earn more than 2300.
Q231. WAQTD employee name, employee SAL, manager name, manager SAL if employee is
earning more than his manager.
Q232. WAQTD employee name, employee HIREDATE, manager name, manager HIREDATE if
employee is hired before his manager.
Q233. WAQTD employee name & his managers name if employee is hired in the year 1980 and
manager is hired in the year 1981.
Q234. WAQTD employee name & managers name if employee & manager both hired in the year
1987.
Q235. WAQTD employee name & manager name if employee & manager both hired in the month
of DEC.
Q236. WAQTD employee name & manager name if employee is earning more than 2900 &
manager is earning more than 3000.
Q237. WAQTD employee name, manager name If employee is working as ANALYST & manager is
working as actual manager.
E2.JOB = ‘MANAGER’;
Q238. WAQTD employee name & manager’s name if employee is earning less than 1000 &
manager in department number 30.
E2.DEPTNO = 30;
Q239. WAQTD employee name, employee LOC, manager’s name, & manager’s LOC.
E2.DEPTNO = D2.DEPTNO;
Q240. WAQTD employee name, employee LOC, manager name, manager LOC if employee is
working as a ‘CLERK’ & manager is working in ‘DALLAS’.
Q241. WAQTD employee name, employee LOC, manager name, manager LOC if employee working
as ‘SALESMAN’ in ‘SALES’ department & manager is working as actual manager in ‘CHICAGO’.
Q242. WAQTD employee name, employee DNAME, manager name, manager DNAME if employee
is hired in the year 81 working as ‘CLERK’ & manager working in ‘SALES’ department.
Q243. WAQTD employee name, employee LOC, manager name, manager LOC if employee is
working in DEPTNO 10 or 20 and hired after 1982 & manager is working as actual manager in
‘RESEARCH’ department.
Q244. WAQTD employee name, employee LOC, manager name, manager LOC if employee is hired
after ‘JONES’ into ‘SALES’ department & manager is earning less than ‘KING’ in ‘CHICAGO’.
FROM EMP E1
FROM EMP E2
Q245. WAQTD employee name, employee DNAME, manager name, manager DNAME if employee
is earning more than ‘ALLEN’ in ‘ACCOUNTING’ department & manager is working as a
‘PRESIDENT’ in ‘NEW YORK’.
FROM EMP E1
Q246. WAQTD employee name, manager name, along with manager’s manager name.
E2.MGR = E3.EMPNO;
A.SAL<B.SAL
GROUP BY A.DEPTNO
ORDER BY A.DEPTNO;
Q248. WAQTD employee name, manager name, manager’s manager name along with their
DNAME.
FROM EMP E1, EMP E2, EMP E3, DEPT D1, DEPT D2, DEPT D3
E3.DEPTNO = D3.DEPTNO;
P a g e | 93
Q249. WAQTD employee name, managers name & manager’s manager name along with their
DNAME if employee is earning more than 1000 & manager earns more than ‘ALLEN’ & Manager’s
manager working in ‘NEW YORK’ or ‘CHICAGO’.
FROM EMP E1, EMP E2, EMP E3, DEPT D1, DEPT D2, DEPT D3
FROM EMP E2
Q250. WAQTD employee name, managers name & manager’s manager name along with their LOC
if the employees hired before ‘MARTIN’ & manager working in ‘ACCOUNTING’ or ‘SALES’
department & manager’s manager earning SAL more than ‘SMITH’.
FROM EMP E1, EMP E2, EMP E3, DEPT D1, DEPT D2, DEPT D3
FROM EMP E1
FROM EMP E3
P a g e | 94
OUTER JOIN
In outer join, we get the matched records along with the unmatched records.
• LEFT OUTER JOIN: In left outer join, we get the unmatched records along with the matched
records from left table.
SELECT *
FROM EMP E, DEPT D
WHERE E.DEPTNO = D.DEPTNO (+)
Syntax:
• RIGHT OUTER JOIN: It is used to get the unmatched records along with the matched records
from right table.
P a g e | 95
SELECT *
FROM EMP E, DEPT D
WHERE E.DEPTNO (+) = D.DEPTNO;
Syntax:
• FULL OUTER JOIN: It is used to get the unmatched records along with the matched records
from both the table.
P a g e | 96
Syntax:
NATURAL JOIN
Syntax:
P a g e | 97
Example: 1. SELECT *
2. SELECT *
PSEUDO-COLUMN
Pseudo Column is the false columns that are present in each & every table & it must be called Explicitly.
TYPES OF PSEUDO-COLUMN
• ROWID
• ROWNUM
ROWID: ROWID is an 18-digit address in which records are present or, stored in a memory.
NOTE:
ROWNUM
SELECT *
FROM EMP
SELECT *
FROM EMP
SELECT *
FROM EMP
SELECT *
FROM EMP
WHERE ROWNUM = 1;
SELECT *
FROM EMP
SELECT *
FROM EMP)
WHERE SLNO = 3;
SELECT *
FROM EMP)
WHERE SLNO = 5;
FROM EMP)
WHERE SLNO = 7;
Q259. WAQTD 1st, 3rd, 5th, & 8th records from EMP table.
SELECT *
FROM EMP)
SELECT ENAME
P a g e | 101
FROM EMP)
SELECT *
FROM EMP
SELECT *
FROM EMP
WHERE ROWNUM<=3;
SELECT SAL
FROM EMP
WHERE SLNO = N;
SELECT SAL
FROM EMP
WHERE SLNO = N;
SELECT SAL
FROM EMP
WHERE SLNO = 7;
SELECT ENAME
FROM EMP
FROM EMP
Q266. WAQTD names of an employee who are earning 2nd maximum salary.
SELECT ENAME
FROM EMP
FROM EMP
SELECT DNAME
FROM EMP
1. INSERT
2. UPDATE
3. DELETE
Consider this BONUS table, as an example. INSERT, UPDATE & DELETE the records in SQL.
BONUS
INSERT
Syntax:
Use COMMIT at the end, to save the inserted values of the table. If not used, the inserted data in the
table gets erased once SQL is closed.
UPDATE
Syntax:
P a g e | 107
UPDATE TABLE_NAME
[WHERE <FILTER_CONDITION>]
DELETE
Syntax:
DELETE
FROM TABLE_NAME
[WHERE <FILTER_CONDITION>];
Example: DELETE
FROM BONUS
SELECT MAX(SAL)
FROM EMP E
SELECT DNAME
FROM DEPT
FROM EMP)
Q270. WAQTD name of employees working as ‘SALESMAN’ in same DEPT as that of BLAKE & he gets
commission & his MANAGER hired in month of ‘MAY’ in ‘CHICAGO’ & manager’s manager earning
more than ‘SCOTT’ in ‘NEW YORK’.
SELECT E1.ENAME
FROM EMP E1, EMP E2, EMP E3, DEPT D1, DEPT D2, DEPT D3
FROM EMP E1
E1.COMM IS NOT NULL AND E2.HIREDATE LIKE ‘%MAY%’ AND D2.LOC = ‘CHICAGO’ AND
FROM EMP E3
1. CREATE
2. RENAME
3. ALTER
4. TRUNCATE
5. DROP
P a g e | 110
CREATE
Syntax:
);
);
P a g e | 111
RENAME
Syntax:
ALTER
Syntax:
1. TO ADD A COLUMN:
ALTER TABLE_NAME
ADD COLUMN_NAME DATATYPE [NULL/NOT NULL];
2. TO DROP A COLUMN:
ALTER TABLE TABLE_NAME
DROP COLUMN_NAME;
TRUNCATE
Syntax:
DROP
Syntax:
Syntax:
TO BEFORE DROP
[RENAME TO NEW_NAME];
Syntax:
1. COMMIT
2. SAVEPOINT
3. ROLLBACK
COMMIT
Syntax: COMMIT;
SAVEPOINT
ROLLBACK
Syntax: ROLLBACK;
ROLLBACK TO
P a g e | 114
It has 2 statements:
1. GRANT
2. REVOKE
GRANT
Syntax:
TO USER_NAME;
TO SCOTT;
REVOKE
Syntax:
FROM USER_NAME;
FROM SCOTT;
FROM EMP;
P a g e | 115
FROM EMP;
Q273. WAQTD details of an employee whose name starts with character ‘A’ using single row
function.
SELECT *
FROM EMP
Q274. WAQTD details of an employee whose designation ends with character ‘N’.
SELECT *
FROM EMP
Q275. WAQTD names of an employee whose name starts with character ‘A’ or ‘S’
SELECT ENAME
FROM EMP
P a g e | 116
SELECT *
FROM EMP
Q277. WAQTD names of an employee along with number of characters they are having in their
name.
FROM EMP;
Q278. WAQTD number of employees having exactly 4 characters in his name using single row
function.
FROM EMP
Q279. WAQTD name in uppercase, designation in lowercase if their name has five characters &
getting 3-digit salary.
FROM EMP
**Q280. WAQTD first character & last character of employee name in uppercase & rest of the
characters in lowercase.
FROM EMP;
**Q281. WAQTD first half of employee name in uppercase & second half in lowercase & reverse.
FROM EMP;
SELECT *
FROM EMP
SELECT *
FROM EMP
Q284. WAQTD details of an employee hired in a month of OCTOBER, NOVEMBER & DECEMBER.
P a g e | 118
SELECT *
FROM EMP
OR,
SELECT *
FROM EMP
Q285. WAQTD details of an employee if their name having character ‘A’ using Single Row
Function.
SELECT *
FROM EMP
Q286. WAQTD details of an employee who is having at least two ‘A’ in his name using single row
function.
SELECT *
FROM EMP
FROM EMP;
Explanation of Execution:
FUNCTIONAL DEPENDENCY
Let us consider the relation ‘R’ with 2 attributes ‘X’ & ‘Y’ respectively in which attribute ‘X’
determines attribute ‘Y’.
OR, in other words, ‘Y’ is dependent on ‘X’, there exists Functional Dependency.
R → {X, Y}
X→Y
Y is dependent on X.
If all the attribute in a relation is determined by a single attribute which is a Key Attribute, then there
exists Total Functional Dependency.
R → {A, B, C, D}
A→B
P a g e | 120
A→C
A→D
A → {B, C, D}
For a partial functional dependency to exists their must a Composite Key Attribute.
One of the Attribute in Composite Key relation determines another attribute separately & this is
known as Partial Functional Dependency.
R → {A, B, C, D}
(A, B) → (C, D)
B → {C}
R → {A, B, C, D}
A→B
D→C
A→D
A→C
2. We can’t get back the table We can get back the table We can get back the
that is truncated. that is dropped by using records that is deleted by
FLASHBACK. using ROLLBACK.
3. SYNTAX: SYNTAX: SYNTAX:
TRUNCATE TABLE DROP TABLE table_name DELETE
table_name FROM TABLE
WHERE [conditions]
***NORMALIZATION
NOTE:
1NF
2NF
NOTE:
If the table consists of partial functional dependency, then the attributes responsible are removed
from the table.
3NF
NOTE:
If the table consists of transitive functional dependency, then the attributes responsible are
removed from the table.
P a g e | 123
P a g e | 124
P a g e | 125
P a g e | 126