SQL Notes (201) .
SQL Notes (201) .
What is an Application?
" Is a list of programs which helps us to perform some a
particular task/action "
Example: WhatsApp
MS office
Web browser [ CHROME, MOZILLA] Facebook
Instagram
GTA Vice city
Types of Applications:
Advantages:
• Faster in access.
• Secured from Data hacking and virus.
Disadvantages:
• Single user access at a time.
•
New Section 1 Page 1
• Installation is required
2.WEB APPLICATION:
Advantages:
• Easy to access and faster in access if the bandwidth is
more
• Data security from data hacking and virus
• Data sharing is possible
• Maintenance is not so tough
• Multiple users can access the application.
Disadvantages:
• Installation is still required at client's place
• If the server goes down no one can access the application
EXAMPLE:
HISTORY OF SQL
1) SQL WAS DESIGNED BY ‘RAYMOND BOYCE’ IN THE
YEAR 1970.
2) FATHER OF SQL IS KNOWN AS RAYMOND BOYCE.
3) E.F.CODD DESGINED RELATIONAL MODEL IN THE YEAR
OF 1970 (SYSTEM.R).
4) E.F CODD IS CALLED BY CO-FATHER OF SQL.
5) IN OLDEN DAYS SQL IS CALLED BY SEQUEL .
S→SIMPLE
E→ENGLISH
QUE→QUERY
L→ LANGUAGE
6) ONCE ANSI(AMERICAN NATIONAL STANDARD
INSTITUTE) TOOK OVER AND THEY WAS CHAGNED THE
NAME AS SQL.
DATA:
" Data is a Raw fact which describe the property of an object "
Properties or Attributes object or entity
HIEGHT: 10CM
CAPACITY: 500ML
DATA BASE
"Data base is place or media which we can store the data in systematic and organized manner"
DATABASE
QUERY
LANGUAGE
FILE
DATABASE SOFTWARE
A B C D E
DATA
STRUCTERD
QUERY
LANGUAGE
➢ RDBMS a type of the DBMS which is used to store the data in the Form of table
➢ We can use to communicate or interact RDBMS by using SQL (Structured Query Language)
EXAMPLE:
NAME
A
B
C
D
E
ASSINMENT:
1)DIFFERENCE BETWEEN THE DBMS AND RDBMS
krish_sql_techie
RELATIONAL MODEL
➢ Relational Model was designed by E.F. CODD
➢ In Relational model we store the Data in the form of Rows and Columns
Any DBMS which follows Relational model becomes on RDBMS
OR
Any DBMS which follows E.F.CODD becomes on RDBMS
Table:
Table is logical organization of Data which consists of Rows and columns
Row
/
Record
/ CELL
Tuple
Columns:
➢ A columns is also known as attributes OR field.
➢ A column is used to represent property of all the entity.
Row:
➢ Row is also known as Record or Tuples.
➢ Row is used to represent all properties of a single entities.
Cell:
➢ Cell is the smallest unit of the table in which we store the data.
➢ The interaction of rows and columns generate cells.
➢ The data entered into a cell must be "single valued data" (atomic)
➢ According to E.F. CODD we can store the data in multiple table, It necessary are can established the
connection between the table with the help of “key attribute”
➢ In RDBMS are store everything in the form of table including “META DATA”
DATA TYPE:
"DATATYPE is used to specify/ determine the type /kind of data that will be stored in a particular
memory allocation."
1) CHAR:
"In CHAR data type we can store characters such as"
Example: 'A-Z' , ‘a-z’ , ‘0-9’ and Special character ( '$', '#', '%', '&')
Size:
→ It is used to specify number of characters that can be stored
→ we can store a max of 2000 characters
→ character must always be with in single quote (' ')
Ex: ‘ABC’ ,‘Raju’ , ‘A’
➢ Character datatype follows "Fixed length memory allocation"
➢ Default size of char is one
Example: CHAR(8)
UNUSED MEMORY/
WASTAGE MEMORY
1 2 3 4 5
K R I S H
6 7 8
USED MEMORY
2) VARCHAR/ VARCHAR2:
"In VARCHAR data type we can store characters such as"
Example: 'A-Z' , ‘a-z’ , ‘0-9’ and Special character ( '$', '#', '%', '&')
Size:
→ It is used to specify number of characters that can be stored
→ we can store a max of 2000 characters
→ character must always be with in single quote (' ')
Ex: ‘ABC’ ,‘Raju’ , ‘A’
➢ VARChar datatype follows "variable length memory allocation" , default size of varchar is zero
Example: VARCHAR(8)
UNUSED MEMORY/
FREE MEMORY
1 2 3 4 5
K R I S H
6 7 8
USED MEMORY
NOTE:
VARCHAR2 is nothing but update version of varchar
ASSIGNMENT
3.NUMBER
' This number datatype is used to store numeric value'
SYNTAX OPTIONAL
NUMBER(Precision, [scale])
Precision
□ Precision is used to determine the number of digit used to store 'Integer value'
□ The range of the precision is 1 to 38
Scale
□ Scale is used to determine the number of digit used to store Decimal (float) value
within the precision
□ The default value of scale is 'zero'
□ The range of scale is -84 to 127
Examples :
1 NUMBER(3) +/-999
2 NUMBER(4) +/-9999
3 NUMBER(5,2) +/-999.99
PRECISION SCALE
4.DATE
The date datatype is used to store the date in specific format given by oracle
SYNTAX: DATE
Format:
1) 'DD-MON-YY' '28-JUN-21'
OR
2) 'DD-MON-YYYY' '28-JUN-2021'
5.LARGE OBJECT
SYNTAX
Binary large object (BLOB) is used to store the binary values of "IMAGES, MP3,
MP4, PDF" etc. up-to 4GB of size
SYNTAX
CONSTRAINTS :
Types of Constraints :
1. UNIQUE
2. NOT NULL
3. CHECK
4. PRIMARY KEY
5. FOREIGN KEY .
1. UNIQUE : "It is used to avoid duplicate values into the column ".
3. CHECK : "It is an extra validation with a condition If the condition is satisfied then the value is
accepted else Rejected ".
EXAMPLE: CHECK(LENGTH(PH_NO))=10
CHECK (PERCENTAGE>60)
4. PRIMARY KEY : "It is a constraint which is used to identify a record Uniquely from the
table" .
5. FOREIGN KEY : "It is used to establish a connection between the The tables "
Example :
EMP
PK
CHECK(SALARY> CHECK(LENTH(PHONE)=
0) 10)
NOT NULL NOT NULL NOT NULL NOT NULL NOT NULL
UNIQUE UNIQUE
EID NAME SALARY DOJ PHONE
NUMBER(4) VARCHAR(10) NUMBER(6,2) DATE NUMBER(10)
123 RAJU 5000.00 '26-JUN-1998' 1234567890
321 PRADEEP 6000.00 '28-JAN-2001' 3214567890
3 C 35000 20
4 D 50000 2 10
Child / accepter
DEPT
DNO DNAME LOC
10 D1 L1
20 D2 L2
ASSIGNMENT :
1. Differentiate between Primary key and Foreign key .
NOTE : NULL
Null Is a keyword which is used to represent Nothing / Empty Cell.
Characteristics of Null :
Ex:
emp 200*null= null
200+null= null
eid ename sal comm 200-null= null
200+ null= null
1 a 100 30
200=null (not possible)
2 b 200
STATEMENTS
" DQL is used to retrieve/Read/fetch the data from the database " .
It has 4 statements :
1. SELECT
2. PROJECTION
3. SELECTION
4. JOIN
SELECT :"It is used to retrieve the data from the table and display it.
PROJECTION :
"It is a process of retrieving the data by selecting only the columns is known as
Projection " .
➢ In projection all the records / values present in a particular columns are by default
selected .
SELECTION :
"It is a process of retrieving the data by selecting both the columns and rows is known
as Selection " .
JOIN :
"It is a process of retrieving the data from Multiple tables simultaneously is known as
Join " .
PROJECTION
➢ "It is a process of retrieving the data by selecting only the columns is known as Projection " .
➢ In projection all the records / values present in a particular columns are by default selected .
SYNTAX :
ORDER OF EXECUTION
1. FROM Clause
2. SELECT Clause
STUDENT SNAME
SID SNAME BRANCH PER A
1 A ECE 60 B
2 B CSE 75 C
D
3 C ME 50
E
4 D ECE 80
F
5 C CSE 75
6 E CIVIL 95
FROM CLAUSE:
➢ FROM Clause starts the execution .
➢ For FROM Clause we can pass Table Name as an argument .
➢ The job of FROM Clause is to go to the Database and search for the table and put the table
under execution .
SELECT CLAUSE
NOTE
Example
➢ WAQTD student id and student names for all the students.
EMP Table :
DEPT :
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
DISTINCT Clause
" It is used to remove the duplicate or repeated values from the Result table ".
EXPRESSION
"Any statement which gives result is known as Expression ". Expression is a combination Operand
and Operator .
Operator : These are the Symbols which perform some Operation on The Operand .
OPERAND
Example : 5 * 10
OPERATOR
EMP
EID ENAME SAL
1 A 100
2 B 200
3 C 100
➢ WAQTD all the details of the employee along with annual salary
ALIAS
"It is an alternate name given to a Column or an Expression In the result table " .
FORMAT : ANNUAL_SALARY
"ANNUAL SALARY"
COMMANDS ON SQL*Plus :
1. CLEAR SCREEN [ CL SCR ] : To clear the screen
2. SET LINES 100 PAGES 100 : To set the dimensions of the output page .
3. EXIT / QUIT : To Close the Software .
4. When account is Locked !!!
Log in as SYSTEM
Password Oracle1234
SELECTION:
"It is a process of retrieving the data by selecting both the columns and rows is known as
Selection "
SYNTAX :
ORDER OF EXECUTION
1. FROM
2.WHERE
3.SELECT
WHERE Clause
EXAMPLE:
3. WAQTD all the details of the employees whose salary is Less than 1000 rupees .
7. WAQTD name sal and hiredate of the employees who were Hired before 1985 .
SELECT ENAME,SAL,HIREDATE
FROM EMP
WHERE HIREDATE < '01-JAN-1985';
8. WAQTD name sal and hiredate of the employees who were Hired after 1985 .
9. WAQTD name of the employees who was hired on Valentine's day 2021 .
OPERATORS IN SQL
1) ARITHEMATIC OPERATORS :- ( + , - , * , / )
2) CONCATENATION OPERATOR :- ( || )
3) COMPARISION OPERATORS :- ( = , != or <> )
4) RELATIONAL OPERATOR :- ( > , < , >= , <= )
5) LOGICAL OP : ( AND , OR , NOT )
6) SPECIAL OPERATOR :-
1. IN
2. NOT IN
3. BETWEEN
4. NOT BETWEEN
5. IS
6. IS NOT
7. LIKE
8. NOT LIKE
7) SUBQUERY OPERATORS:-
1. ALL
2. ANY
3. EXISTS
4. NOT EXISTS
CONCATENATION Operator :
Symbol : ||
SYNTAX
'STRING1' || 'STRING 2'
NOTE :
We can join 'N' number of strings by using a concatenation operator.
Example:
WAQTD ename the emp working as manager
SELECT ENAME
FROM EMP
WHERE JOB= 'MANAGER';
ENAME
ALLEN
JONES
LOGICAL OPERATORS
➢ AND
➢ OR
➢ NOT
➢ WAQTD name and deptno along with job for the employee working in dept 10 .
➢ WAQTD name and deptno along with job for the employee working as manager in dept 10 .
➢ WAQTD name , deptno , salary of the employee working in dep 20 and earning less than 3000 .
➢ WAQTD name and salary of the employee if emp earns More than 1250 but less than 3000 .
➢ WAQTD name and deptno of the employees if the EMP works in dept 10 or 20 .
➢ WAQTD name and sal and deptno of the employees If emp gets more than 1250 but less than
4000 and works in dept 20 .
➢ WAQTD name , job and deptno of the employees working as clerk or manager in dept 10 .
➢ WAQTD name , job , deptno , sal of the employees working as clerk or salesman in dept 10 or
30 and earning more than 1800 .
SPECIAL OPERATORS :
1. IN
2. NOT IN
3. BETWEEN
4. NOT BETWEEN
5. IS
6. IS NOT
7. LIKE
8. NOT LIKE
IN :It is a multi-valued operator which can accept multiple values At the RHS .
Syntax: Column_Name / Exp IN ( v1 , v2 , . . Vn )
Example :
➢ WAQTD empno , ename and salary of the employees whose empno Is 7902 or 7839 and getting
salary more than 2925.
SELECT EMPNO,ENAME,SAL
FROM EMP
WHERE EMPNO IN (7902,7839) AND SAL>2925;
NOT IN : It is a multi-valued operator which can accept multiple values At the RHS . It is similar
to IN op instead of selecting it Rejects the values .
Example :
➢ WAQTD name and deptno of all the employees except the emp Working in dept 10 or 40 .
SELECT ENAME,DEPTNO
FROM EMP
➢ WAQTD name , deptno and job of the employee working in dept 20 but not as a clerk or
manager .
SELECT ENAME,DEPTNO,JOB
FROM EMP
WHERE DEPTNO=20 AND JOB NOT IN ('CLERK' , 'MANAGER');
Example :
➢ WAQTD name and salary of the employees if the emp is earning Salary in the range 1000 to 3000 .
SELECT ENAME,SAL
FROM EMP
WHERE SAL BETWEEN 1000 AND 3000;
➢ WAQTD name and deptno of the employees working in dept 10 And hired during 2019 (the entire
year of 2019) .
SELECT ENAME,DEPTNO
FROM EMP
WHERE DEPTNO IN 10 AND HIREDATE BETWEEN '01-JAN-2019' AND '31-DEC-2019';
➢ WAQTD name , sal and hiredate of the employees hired during 2017 into dept 20 with a salary
greater than 2000 .
Example :
➢ WAQTD name and salary of the employees if the emp is not earning Salary in the range 1000 to
➢ 3000 .
SELECT ENAME,SAL
FROM EMP
WHERE SAL NOT BETWEEN 1000 AND 3000;
➢ WAQTD name , sal and hiredate of the employees who were not hired during 2017 into dept 20
➢ with a salary greater than 2000 .
SELECT ENAME,SAL,HIREDATE
FROM EMP
WHERE HIREDATE NOT BETWEEN '01-JAN-2017' AND '31-DEC-2017' AND DEPTNO IN 20 AND SAL>2000;
Example :
EMP
IS NOT : "It is used to compare the values with NOT NULL ".
➢ WAQTD name , sal and comm of the emp if the emp doesn’t earn commission but gets salary .
ASSIGNMENT QUESTIONS :
➢ Percentile (%)
➢ Underscore ( _ )
➢ Percentile (%) : it is a special character which is used to match any number of character, any number of
time / number of character.
➢ Underscore ( _ ) : it is a special character which is used to match exactly once but any character.
Example :
➢ WAQTD names of the employees who have character 'S' in their names .
SELECT ENAME FROM EMP WHERE ENAME LIKE '%S%';
NOTE:
'%A' SELECT LAST CHAR
'A%' SELECT FIRST CHAR
'%A%' SELECT ANYWHERE CHAR
➢ WAQTD names that starts with 'J' and ends with 'S' .
SELECT ENAME FROM EMP WHERE ENAME LIKE 'J%S';
➢ WAQTD names of the employee if the emp has char 'A' as his second character .
SELECT ENAME FROM EMP WHERE ENAEM LIKE '_A%'
➢ WAQTD names of the employee if the emp has char 'A' as his Third character .
➢ WAQTD names of the employee if the emp has char 'A' as his second character and 'S' is last character .
➢ WAQTD names of the employee if the emp has char 'V' present atleast 2 times .
SELECT ENAME FROM EMP WHERE ENAME LIKE '%V%V%';
➢ WAQTD names of the employee if the emp name starts with 'A' and ends with 'A' .
➢ WAQTD names and sal of employee if the emp annual salary's last 3 digit is 200 rupees
SELECT ENAME ,SAL FROM EMP WHERE SAL*12 LIKE '%200';
➢ WAQTD names of the employee if the emp has not a char 'A' as his Third character .
FUNCTIONS
"Functions Are a block of code or list of instructions which are used to perform a specific task ".
TWO TYPES
SRF() MRF()
OUTPUT input
INPUT
1 1
2
3
2 OUTPUT
4
N
N
ENAME SAL
ENAME SAL
ALLEN 100
ALLEN 100
JONES 200
JONES 200
MR.ALEX 300
MR.ALEX 300 LENGTH(ENAME)
5
5 SELECT MAX(SAL)
SELECT LENGTH(ENAME) 7 FROM EMP;
FROM EMP;
List of MRF ( )
NOTE :
➢ Multi row functions can accept only one argument , i.e. a Column_Name or an Expression
Examples :
➢ WAQTD number of employees hired after 1982 and before 1985 into Dept 10 or 30 .
➢ WAQTD number of employees getting commission .
➢ WAQTD maximum salary given to employees if the emp has character 'S' in the name and works as a
Manager in dept 10 with as salary of more than 1800 .
➢ WAQTD number of employees working in dept 10 or 30 and getting commission without the salary .
➢ WAQTD maximum salary given to a manager working in dept 20 and also his comm must be greater than his
salary .
ASSIGNEMENT ON MRF( )
GROUPING:GROUP-BY Clause
NOTE :
SYNTAX:
ORDER OF EXECUTION:
1. FROM
2. WHERE (if used) [ROW-BY-ROW]
3. GROUP BY [ROW-BY-ROW]
4. SELECT [GROUP-BY-GROUP]
EMP
EID ENAME SAL DEPTNO
1 A 100 20
2 B 200 10
3 C 300 30
4 D 100 10
5 E 200 10
6 A 400 30
7 C 500 20
8 F 200 30
Example :
10
Questions :
➢ WAQTD number of employees working in each dept except the Employee working as analyst .
➢ WAQTD maximum salary given to each job .
➢ WAQTD number of employees working in each job if the employees Have character 'A' in their names .
WAQTD number of employees getting commission in each dept .
NOTE:
SYNTAX:
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]
Example :
Questions :
NOTE :
WHERE HAVING
Where clause is used to Filter the Having clause is used to Filter
records the groups .
Where clause executes row By Having clause executes Group by
row . group
In Where Clause we cannot Use Can use MRF( ).
MRF( )
1. WAQTD DNO AND NUMBER OF EMP WORKING IN EACH DEPT IF THERE ARE ATLEAST 2 CLERKS IN EACH DEPT
2. WAQTD DNO AND TOTAL SAALARYNEEDED TO PAY ALL EMP IN EACH DEPT IF THERE ARE ATLEAST 4 EMP IN
EACH DEPT
3. WAQTD NUMBER OF EMP EARNING SAL MORE THAN 1200 IN EACH JOB AND THE TOTAL SAL NEEDED TO PAY
EMP OF EACH JOB MUST EXCEES 3800
4. WAQTD DEPTNO AND NUMBER OF EMP WORKING ONLY IF THERE ARE 2 EMP WORKING IN EACH DEPT AS
MANAGER .
5. WAQTD JOB AND MAX SAL OF EMP IN EACH JOB IF THE MAX SAL EXCEEDS 2600
6. WAQTD THE SALARIES WHICH ARE REPEATED IN EMP TABLE
7. WAQTD THE HIREDATE WHICH ARE DUPLICATED IN EMP TABLE
8. WAQTD AVG SALARY OF EACH DEPT IF AVG SAL IS LESS THAN 3000
9. WAQTD DEPTNO IF THERE ARE ATLEAST 3 EMP IN EACH DEPT WHOS NAME HAS CHAR 'A' OR 'S' .
10. WAQTD MIN AND MAX SALARIES OF EACH JOB IF MIN SAL IS MORE THAN 1000 AND MAX SAL IS LESS THAN
5000 .
ORDER BY CLAUSE
Order by clause is used to arrange the records either in ascending order or descending order.
SYNTAX:
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 [ROW-BY-ROW]
EXAMPLE
SAL
120
300
50
SELECT SAL
FROM EMP
ORDER BY SAL [ASC];
SAL
50
120
300
SAL
300
120
50
SUB-QUERY:
"A query written inside another query is known As sub query "
Working Principle :
Case 1:
Whenever we have Unknowns present in the Question, We use sub query to find The Unknown values.
Example :
EMP
EID ENAME SAL DEPTNO
1 ALLEN 1000 20
2 BLAKE 2000 10
3 CLARK 3000 30
4 MILLER 1500 10
5 SMITH 2500 10
NOTE:
• In the Inner Query / Sub Query we cannot select more than One column .
• The corresponding columns need not be same , but the datatypes of those has to be same .
ASSIGNMENT ON CASE 1
SUB-QUERY
CASE-2:
Whenever the data to be selected and the condition to be executed are present in different tables we use Sub Query .
Example :
EMP DEPT
EID ENAME SAL DEPTNO DEPTNO DNAME LOC
1 ALLEN 1000 20 10 D1 L1
2 BLAKE 2000 10 20 D2 L2
3 CLARK 3000 30 30 D3 L3
4 MILLER 1500 10
5 ADAMS 2500 20
EXAMPLE 2:
CUSTOMER PRODUCT
CID CNAME CNO PID PID PNAME PRICE DISCOUNT
1 SMITH 12345 101 101 Iphone 11 51000 0
2 JONES 12349 102 102 Iphone 12 74000 1000
3 ALLEN 12346 103 103 iPad 40000 100
ASSIGNMENT ON CASE 2 :
1.WAQTD DNAME OF THE EMPLOYEES WHOS NAME IS SMITH
2.WAQTD DNAME AND LOC OF THE EMPLOYEE WHOS ENAME IS KING
3.WAQTD LOC OF THE EMP WHOS EMPLOYEE NUMBER IS 7902
4.WAQTD DNAME AND LOC ALONG WITH DEPTNO OF THE EMPLOYEE WHOS NAME
ENDS WITH 'R' .
5.WAQTD DNAME OF THE EMPLOYEE WHOS DESIGNATION IS PRESIDENT
6.WAQTD NAMES OF THE EMPLOYEES WORKING IN ACCOUNTING DEPARTMENT
7.WAQTD ENAME AND SALARIES OF THE EMPLOYEES WHO ARE WORKING IN THE
LOCATION CHICAGO
8.WAQTD DETAILS OF THE EMPLOYEES WORKING IN SALES
9.WAQTD DETAILS OF THE EMP ALONG WITH ANNUAL SALARY IF EMPLOYEES
ARE WORKING IN NEW YORK
10. WAQTD NAMES OF EMPLOYEES WORKING IN OPERATIONS DEPARTMENT
ASSIGNMENT ON CASE 1 & 2
1.WAQTD NAMES OF THE EMPLOYEES EARNING MORE THAN SCOTT IN ACCOUNTING DEPT
2.WAQTD DETAILS OF THE EMPLOYEES WORKING AS MANAGER IN THE LOCATION CHICAGO
3.WAQTD NAME AND SAL OF THE EMPLOYEES EARNING MORE THAN KING IN THE DEPT ACCOUNTING
4.WAQTD DETAILS OF THE EMPLOYEES WORKING AS SALESMAN IN THE DEPARTEMENT SALES
5.WAQTD NAME , SAL , JOB , HIREDATE OF THE EMPLOYEES WORKING IN OPERATIONS DEPARTMENT AND
HIRED BEFORE KING
6.DISPLAY ALL THE EMPLOYEES WHOSE DEPARTMET NAMES ENDING 'S'.
7.WAQTD DNAME OF THE EMPLOYEES WHOS NAMES HAS CHARACTER 'A' IN IT .
8.WAQTD DNAME AND LOC OF THE EMPLOYEES WHOS SALARY IS RUPEES 800 .
9.WAQTD DNAME OF THE EMPLOYEES WHO EARN COMISSION
10.WAQTD LOC OF THE EMPLOYEES IF THEY EARN COMISSION IN DEPT 40
EXAMPLE:
EMP DEPT
EID ENAME SAL DEPTNO DEPTNO DNAME LOC
1 ALLEN 1000 20 10 D1 L1
2 BLAKE 2000 10 20 D2 L2
3 CLARK 3000 30 30 D3 L3
4 MILLER 1500 10
5 SMITH 2500 10
➢ If the sub query returns exactly 1 record / value we call it as Single Row Sub
Query.
➢ If it returns only 1 value then we can use the normal operators Or the Special
Operators to compare the values.
EXAMPLE
➢ If the sub query returns more than1 record / value we call it as Multi Row Sub
Query.
➢ If it returns more than 1 value then we cannot use the normal operators We have
to use only Special Operators to compare the values.
SELECT DNAME
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE ENAME IN('ALLEN' , 'SMITH');
Note :
1. WAQTD Ename and salary of the employees earning more than Employees of
dept 10.
EMP
EID ENAME SAL DEPTNO
1 ALLEN 1000 20
2 BLAKE 2000 10 = OR IN
3 CLARK 3000 30 <> OR NOT IN
❖ ALL :
"It is special Op used along with a relational Op ( > , < , > = , <= ) to compare the
values present at the RHS".
ALL Op returns true if all the values at the RHS have satisfied the condition .
EAXMPLE
1. WAQTD Ename and salary of the employees earning more than Employees of
dept 10.
SELECT ENAME,SAL (CLARK, 3000)
FROM EMP 2000
WHERE SAL > ALL (SELECT SAL 1500
FROM EMP 2500
1000 WHERE DEPTNO =10);
"It is special Op used along with a relational Op ( > , < , > = , <= ) to compare the
values present at the RHS ".
ANY Op returns true if one of the values at the RHS have satisfied the condition.
Example :
➢ WAQTD ENAME,SAL WHO ARE EARNING SAL MORE THAN ANY ONE
1. WAQTD name of the employee if the employee earns less than ALL The
employees working as salesman .
SELECT ENAME
FROM EMP
WHERE SAL <ALL(SELECT SAL
FROM EMP
WHERE JOB='SALESMAN');
2. WAQTD name of the employee if the employee earns less than At least a
salesman
3. WAQTD names of the employees earning more than ADAMS .
SAL
1000
2000
4000
3000
5000
REMEMBER :
EMP
EMPNO ENAME MGR
1 ALLEN 3
2 SMITH 1
3 JAMES 2
4 KING 3
NOTE:
SELECT *
FROM DEPT
WHERE DEPTNO IN (SELECT DEPTNO
FROM EMP
WHERE MGR IN (SELECT EMPNO
FROM EMP
WHERE ENAME='MILLER'));
SUB QUERY:
JOINS:
"The process of retrieval of data from multiple tables simultaneously is known as JOINS ".
"whenever we have to select attribute from more than one table then we use joins"
Types of JOINS .
In Cartesian Join a record from table 1 will be merged with All the records of table 2
Number of Columns in the Result table : will be equivalent to the summations of columns present in both the tables .
Number of Rows in the Result table : will be equivalent to the product of number of rows present in the both the
tables .
Example
EMP DEPT
ENAME DEPTNO
DNAME DEPTNO
A 30
B 10 D1 10
C 20 D2 20
D3 30
Result Table :
SYNTAX:
1. ANSI [ American National Standard Institute ]
SELECT Column_Name
FROM Table_Name1 CROSS JOIN Table_Name2 ;
2. Oracle
SELECT Column_Name
FROM Table_Name1 , Table_Name2 ;
Example :
INNER JOIN:
"It is used to Obtain only Matching Records " Or " A records which has a Pair " .
EMP DEPT
ENAME DEPTNO DNAME DEPTNO
A 20 D1 10
B 30
D2 20
C 10
D3 30
Result Table :
SYNTAX:
SELECT *
FROM EMP INNER JOIN DEPT
ON EMP.DEPTNO = DEPT.DEPTNO ;
2. Oracle
SELECT Column_Name
FROM Table_Name1, Table_Name2
WHERE <JOIN_CONDITION > ;
SELECT *
FROM EMP , DEPT
WHERE EMP.DEPTNO = DEPT.DEPTNO ;
2. WAQTD ename and loc for all the employees working as Manager .
SEELECT ENAME,LOC
FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO AND JOB='MANAGER';
3. WAQTD ename , sal and dname of the employee working as Clerk in dept 20 with a salary of more than 1800 .
SELECT ENAME,SAL,DNAME
FROM EMP, DEPT
WHERE EMP.DEPTNO =DEPT.DEPTNO AND JOB='CLERK' AND DEPTNO=20 AND SAL>1800;
SELECT ENAME,SAL,DNAME
FROM EMP, DEPT
WHERE EMP.DEPTNO =DEPT.DEPTNO AND JOB='CLERK' AND DEPT.DEPTNO=20 AND SAL>1800;
4. WAQTD ename,deptno , dname and loc of the employee earning more than 2000 in New York .
SELECT ENAME,DNAME,LOC,DEPTNO
FROM EMP,DEPT
WHERE EMP . DEPTNO=DEPT.DEPTNO AND SAL>2000 AND LOC='NEW YARK';
SELECT ENAME,DNAME,LOC,EMP.DEPTNO
FROM EMP,DEPT
WHERE EMP . DEPTNO=DEPT.DEPTNO AND SAL>2000 AND LOC='NEW YARK';
OUTER JOIN:
"It is used to obtain Un-Matched Records of Left Table Along with Matching Records ".
EMP DEPT
ENAME DEPTNO DNAME DEPTNO
A 20 D1 10
B 30 D2 20
C 10 D3 30
D 60 D4 40
Left Right
Result Table :
WAQTD names and dname of all the employees even though the employees Don’t work in any
dept .
SELECT ENAME,DNAME
FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO (+);
ENAME DNAME
A D2
C D1
B NULL
D NULL
"It is used to obtain Un-Matched Records of Right Table Along with Matching Records ".
SYNTAX:
ANSI [ American National Standard Institute ]
SELECT Column_Name
FROM Table_Name1 RIGHT[OUTER] JOIN Table_Name2
ON < JOIN_CONDITION> ;
SELECT *
FROM EMP RIGHT JOIN DEPT
ON EMP.DEPTNO = DEPT.DEPTNO ;
Oracle
SELECT Column_Name
FROM Table_Name1 , Table_Name2
WHERE Table1.Col_Name (+) = Table2.Col_Name ;
SELECT *
FROM EMP , DEPT
WHERE EMP.DEPTNO(+) = DEPT.DEPTNO ;
Example :
DEPT EMP
New Section 1 Page 69
DEPT EMP
DNAME DEPTNO ENAME DEPTNO
D1 10 A 20
D2 20 B 30
D3 30 C 10
D4 40 D Null
Left Right
Result Table :
WAQTD names and dname of all the employees even though the there are no employees in a
dept .
SELECT ENAME,DNAME
FROM EMP,DEPT
WHERE EMP.DEPTNO(+)=DEPT.DEPTNO;
"It is used to obtain Un-Matched Records of both Left & Right Table Along with Matching
Records ".
SYNTAX
1. ANSI [ American National Standard Institute ]
SELECT Column_Name
FROM Table_Name1 FULL [OUTER] JOIN Table_Name2
ON < JOIN_CONDITION> ;
SELECT *
FROM EMP FULL JOIN DEPT
ON EMP.DEPTNO = DEPT.DEPTNO ;
Example :
EMP DEPT
ENAME DEPTNO DNAME DEPTNO
A 20 D1 10
Result Table :
ENAME DEPTNO DNAME DEPTNO MATCHING RECORDS OF BOTH TABLE
A 20 D2 20
C 10 D1 10
B NULL NULL NULL UNMATCHED RECORDS OF LEFT TABLE
D NULL NULL NULL
NULL NULL D3 30
NULL NULL D4 40 UNMATCHED RECORDS OF RIGHT TABLE
WAQTD names and dname of all the employees and depts even though the employees Don’t
work in any dept and a dept having no employees.
SELECT ENAME,DNAME
FROM EMP FULL JOIN DEPT
ON EMP.DEPTNO = DEPT.DEPTNO;
SELF JOIN :
Why ? / When ?
SYNTAX:
ANSI [ American National Standard Institute ]
SELECT Column_Name
FROM Table_Name1 JOIN Table_Name2
ON < JOIN_CONDITION> ;
SELECT *
FROM EMP E1 JOIN EMP E2
ON E1.MGR = E2.EMPNO ;
Oracle
New Section 1 Page 71
Oracle
SELECT Column_Name
FROM Table_Name1T1 , Table_Name2 T2
WHERE < Join_Condition > ;
SELECT *
FROM EMP E1 , EMP E2
WHERE E1.MGR = E2.EMPNO;
EXAMPLE
EMP E1
EMPNO ENAME E1.MGR
1 ALLEN 3
2 SMITH 1
3 MILLER 2
RESULT TABLE;
E1.EMPNO E1.ENAME E1.MGR E2.EMPNO E2.ENAME E2.MRG
1 ALLEN 3 3 MILLER 2
2 SMITH 1 1 ALLEN 3
3 MILLER 2 2 SMITH 1
NOTE :
TO join 'N' number of tables we need to write 'N-1' number of join conditions
➢ WAQTD EMP NAME AND MANAGER NAME AND THEIR SALARIES IF EMPLOYEE
EARNS MORE THAN MANAGER
➢ WAQTD EMP NAME AND MANAGER NAME IF BOTH ARE WORKING IN SAME
JOB
➢ WAQTD EMP NAME AND MANAGER NAME ALONG WITH THEIR ANNUAL
SALARIES IF EMPLOYEE WORKS IN DEPT 10 , 20 AND MANAGER'S SAL IS
GREATER THAN EMPLOYEES SALARY .
"It behaves as INNER JOIN if there is a relation between the given two tables , else it behaves
as CROSS JOIN" .
SYNTAX:
ANSI :
SELECT Col_Name
FROM Table_Name1 NATURAL JOIN Table_Name2;
EXAMPLE
RESULT TABLE:
ENAME DEPTNO DNAME
A 20 D2
B 30 D3
C 10 D1
EMP CUSTOMER
RESULT TABLE
EMP DEPT
E1.MGR=E2.EMPNO
EMP.DEPTNO=DEPT.DEPTNO
SELECT E1.ENAME,DNAME,E2.ENAME
FROM EMP E1,EMP E2,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO AND E1.MGR=E2.EMPNO;
" A query written inside another query such that the outer query and the inner
query are Dependent on each other , this is known as Co-Related Sub-Query ".
WORKING PRINCIPLE :
NOTE :
i. In co-related sub query a Join condition is a must , And must be written only in the
Inner Query .
ii. Co-Related sub query works with the principles of both SUB QUERY & JOINS .
Example :
D1 D2 D3 NULL
10
(20,20)
SELECT DNAME
FROM DEPT 30
FROM EMP
10 WHERE EMP.DEPTNO=DEPT.DEPTNO);
20
30
20
10
40 10
20
10=10 TRUE 30=10 F 20
10=20 F 30=20 F 30
30
10=20 F 30=20 F 40
10=30 F 30=30 T
20=10 F 40=10 F
20=20 T 40=20 F
20=20 T 40=20 F
20=30 F 40=30 F
EX:
1. EXISTS : " Exists Op is a Unary Op ( One Operand ) which can accept one
operand towards RHS and that operand has to be co related sub -query
➢ Exists Op returns true if the Sub Query returns Any value other than Null.
example: 10
d1,d2,d3 (20,20)
SELECT DNAME 30
FROM DEPT NULL
WHERE exists (SELECT DNO
FROM EMP
WHERE EMP.DEPTNO=DEPT.DEPTNO);
10=10 TRUE 30=10 F
10=20 F 30=20 F
10=20 F 30=20 F
10=30 F 30=30 T
20=10 F 40=10 F
20=20 T 40=20 F
20=20 T 40=20 F
20=30 F 40=30 F
2. NOT EXISTS :
"NOT EXISTS operator is a unary op(one operand) which can accept one
operand towards RHS and that operand has to a co related sub query"
SELECT SAL
FROM EMP E1
WHERE ( SELECT COUNT( DISTINCT SAL )
FROM EMP E2
WHERE E1.SAL < E2.SAL ) = N-1 ;
3000
New Section 1 Page 78
3000
2000
4000
5000
SELECT SAL
FROM EMP E1
WHERE ( SELECT COUNT( DISTINCT SAL )
FROM EMP E2
WHERE E1.SAL > E2.SAL ) = N-1 ;
ASSIGNMENT QUESTIONS:
SAL
1000
6000
2000
3000
4000
5000
8000
7000
6000
9000
1. LENGTH( )
2. CONCAT( )
3. UPPER( )
4. LOWER( )
5. INITCAP( )
6. REVERSE( )
7. SUBSTR()
8. INSTR( )
9. REPLACE( )
10. MOD( )
11. TRUNC( )
12. ROUND( )
13. MONTHS_BETWEEN()
14. LAST_DAY()
15. TO_CHAR( )
16. NVL( )
17. ASCII()
18. POWER()
19. SQRT()
20. SIN()
21. TAN()
LENGTH: "It is used to count the number of characters present In the given
string ".
SYNTAX: LENGTH ( 'string' )
Example :
SELECT LENGTH('SMITH')
FROM DUAL;
SELECT LENGTH('KRISHNA')
New Section 1 Page 80
SELECT LENGTH('KRISHNA')
FROM DUAL;
NOTE :
DUAL TABLE
It is a DUMMY table which has 1 col and 1 row .Which is used to output the
result .
○ DESC DUAL ;
○ SELECT * FROM DUAL ;
Example :
SELECT CONCAT('MR' , ENAME)
FROM EMP
WHERE ENAME='SMITH'; MR SMITH
INITCAP( ):"It is used to convert a given string to initial capital letter case ".
NOTE: Length is not mandatory , If length is not mentioned then Consider the
complete string .
Example :
-ve
-7 -6 -5 -4 -3 -2 -1
Q S P I D E R
1 2 3 4 5 6 7
+ve
ENAME OUTPUT
SMITH SM
MILLER MIL
JONES JO
WARD WA
ENAME OUTPUT
SMITH ITH
MILLER LER
JONES NES
WARD RD
NOTE : if the third argument is not mentioned the default Value of it is Null .
9. INSTR ( ):"it is used to obtain the position in which the string is present in the Original
string ".
It is used to search for a string in the Original string if present it returns the POSITION
Else it returns 0 ".
SYNTAX
B A N A N A
1 2 3 4 5 6
Syntax: MOD ( m , n )
ROUND( ) : this function is used to round of the given number to its nearest value
TRUNC( ): "It is similar to ROUND() but it always rounds-off the given number to
the lower value "
SYNATX:TRUNC (NUMBER)
SYSDATE
----------------
'17-JUL-21'
CURRENT_D
-----------------
'17-JUL-21'
SYSTIMESTAMP
LAST_DAY( ): " it is used to Obtain the last day in the particular of the given date" .
New Section 1 Page 88
LAST_DAY( ): " it is used to Obtain the last day in the particular of the given date" .
SYNATX: LAST_DAY(DATE)
TO_CHAR( ) :"It is used to convert the given date into String format based on the
Model given "
SYNTAX : TO_CHAR(DATE,'FROMAT_MODELS')
Format Models :
NVL( ) : [ NULL VALUE LOGIC ] " It is used to eliminate the side effects of using
null in arithmetic operations " .
SYNTAX: NVL(ARGUMENT1,ARGUMENT 2)
Argument 1 : Here write any column / exp which can result In null .
Argument 2 : Here we write a numeric value which will be substituted if argument 1
results in Null , If argument 1 is NOT NULL then the same value will be considered .
SELECT ENAME,SAL+NVL(COMM,0)
FROM EMP;
ENAME SAL+NVL(COMM,0)
ENAME SAL COMM A (500+NVL(100,0)= 500+100=600
A 500 100 B (1000+NVL(NULL,0)= 1000+0=1000
B 1000 NULL C (2000+NVL(200,0))= 2000+200=2200
C 2000 200 D (2000+NVL(NULL,0))=2000+0=2000
D 2000 NULL
RESULT
600
1000
ASCII(STRING)
SYNTAX:
SYNTAX
SQRT(NUMBER)
SYNTAX:
TAN(NUMBER)
SYNATX:
SIN(NUMBER)
ASSIGNEMT
1. List employees whose name having 4 characters
4. Display all the employees whose job ends with string 'MAN'
5. Display first 3 characters of Ename in lower case and rest everything in upper case.
If Ename is 'QSPIDERS' then display this as ‘qspIDERS’
6. Display the result from emp table as below. SMITH is a CLERK and gets salary 2000
Here SMITH is ename column, CLERK is JOB and 2000 is SAL column and rest
everything is literal strings.
" DDL is used to construct an object in the database and deals with the Structure of the Object "
It has 5 statements :
1. CREATE
2. RENAME
3. ALTER
4. TRUNCATE
5. DROP
EXAMPLE 1:
EXAMPLE 2:
TABEL NAME= CUSTOMER (OBJECT)
NO OF COL =5
OR
➢ TO ADD A COLUMN :
➢ TO DROP A COLUMN :
EXAMPLE
ALTER TABLE CUSTOMER
DROP COLUMN EMAIL ;
➢ TO RENAME A COLUMN :
EXAMPLE:
ALTER TABLE PRODUCT
RENAME COLUMN DISCOUNT TO DISC;
➢ EXAMPLE:
ALTER TABEL CUSTOMER
DROP: " IT IS USED TO REMOVE THE TABLE FROM THE DATABASE "
EXAMPLE
FLASHBACK
1. INSERT
2. UPDATE
3. DELETE
EXAMPLE;
INSERT INTO PRODUCT VALUES(11, 'REALME' , 5000.00, 1000.00);
EXAMPLE:
CUSTOMER:
CID CNAME PH_NO ADDRESS PID
NUMBER(3) VARCHAR(15) NUMBER(10) VARCHAR(15) NUMBER(2)
1 DINGA 9874561231 BANGLORE 12
2 DINGI 9874654123 BANGLORE 22
EXAMPLE:
WAQTD UPADTE OF PHONE OF DINGA TO 1234567890
UPADTE CUSTOMER
SET PH_NO=1234567890, ADDRESS = 'MANGLORE'
WHERE CNAME='DINGA';
Syntax: DELETE
FROM Table_Name
[ WHERE stmt ];
CUSTOMER:
CID CNAME PH_NO ADDRESS PID
NUMBER(3) VARCHAR(15) NUMBER(10) VARCHAR(15) NUMBER(2)
1 DINGA 9874561231 BANGLORE 12
2 DINGI 9874654123 BANGLORE 22
EXAAMPLE:
WAQTD REMOVE DINGA FROM THE LIST OF CUSTOMER
DELETE FROM CUSTOMER
WHERE CNAME='DINGA';
5.WAQT hike the salary of the employee to 10% . If employees earn less than
2000 as a salesman .
1.COMMIT : "This statement is used to SAVE the transactions into the DB ".
Syntax: COMMIT ;
EXAMPLE:
CREATE T1
COMMIT;
2.ROLLBACK :
This statement is used to Obtain only the saved data from the DB .
It will bring you to the point where you have committed for the last time .
SYNTAX: ROLLBACK ;
3. SAVEPOINT :
This statement is used to mark the positions or restoration points . (nothing related to DB ) .
ROLLBACK TO S1;
ROLLBACK TO S1
ROLLBACK TO S2
We have 2 statements :
1. GRANT
2. REVOKE
1.GRANT : THIS STATEMENT IS USED TO GIVE PERMISSION TO A USER .
EXAMPLE:
ERROR
FROM SCOTT.EMP
*
ERROR at line 2:
ORA-00942: table or view does not exist
SQL> CONNECT
Enter user-name: SCOTT Enter password: ***** Connected.
SQL> GRANT SELECT ON EMP TO HR;
Grant succeeded.
SQL> CONNECT
Enter user-name: HR
Enter password: *****
Connected.
SQL> SELECT *
2 FROM SCOTT.EMP;
Non key attribute : All the attributes other than key attributes .
Ex : Name , age , gender , dob
Non-prime key attribute : All the key attributes other than Prime key attributes
Ex : mail_id , Aadhar , pan , ration , passport , dl , bank a/c .
example:
name, age, colour, dob, height, weight, job, nationality
properties
FUNCTIONAL DEPENDENCY :
There exists a dependency such that a part of composite key attributes determines
another attribute uniquely .
Customer
CNAME ADDRESS MAIL_ID PHONE_NO
Smith Mysore [email protected]
Miller Bangalore 1001
Scott Mangalore [email protected]
Adams Mysore 2002
Scott Delhi [email protected] 3003
There exists a dependency such that an attribute is determined by a non-key attribute, which is
intern determined by a key attribute .
Customer
CID CNAME PINCODE CITY
1 Smith 560019 Bangalore
2 Miller 560019 Bangalore
3 Scott 312121 Pune
New Section 1 Page 105
3 Scott 312121 Pune
4 Adams 123456 Mumbai
5 Scott 123456 Mumbai
What is Normalization ?
" It is the process of reducing a large table into smaller tables in order to remove
redundancies and anomalies by identifying their functional dependencies is
known as Normalization . "
Or
"The process of decomposing a large table into smaller table is known as Normalization ."
Or
Example
Table
Note : If any Table / entity is reduced to 3NF , then the table is said to be normalized.
QSPIDERS
QID NAME COURSE
1 A JAVA
2 B JAVA , SQL
3 C MT , SQL
1 A MT
QID NAME C1 C2 C3
1 A JAVA MT
2 B JAVA SQL
3 C SQL MT
Note
VIEW
"In SQL view is a virtual table based on the result set of an SQL statement"
SYNTAX
SYNATX 2
NOTE:
To give permission to create view !!
LOGIN AS SYSTEM
CONNECT ;
USER : SYSTEM
PWD: Oracle1234
EXAMPLE :
COMMENTS
It has 2 comments
comments starts and end within single line are called as single line comments (individual line
comments).
Single line comments start with -- and [ends with :] __WAQTD ALL THE DETAILS OF THE EMP
SELECT *
syntax FROM EMP;
Example:
--select all the details of the employees :
SELECT * FROM EMP;
Comments starts with first line but ends with different line are called multi lines comments
syntax
/*multi
line
comments */
select column name/ expression
from table name
Example:
/*select all the
details of the
employees : */
SELECT * FROM EMP;
SET OPERATOR
1. UNION
2. UNION ALL
3. INTERSECT
4. MINUS
UNION
The union operator could to be used to find the result set or combination of
two/more table without returning any duplicate rows
Note :
Syntax:
SELECT COL_NAME
FROM TABLE _NAME1
[WHERE<FILTER CODITION>]
UNION
SELECT COL_NAME
FROM TABLE_NAME2
[WHERE<FILTER CODITION>];
Order of execution
1. From
2. [Where] (if used) [ROW BY ROW]
3. Select
The union all operator could to be used to find the result set or combination of
two/more table including returning any duplicate rows
Note :
Syntax:
SELECT COL_NAME
FROM TABLE _NAME1
[WHERE<FILTER CODITION>]
UNION ALL
SELECT COL_NAME
FROM TABLE_NAME2
[WHERE<FILTER CODITION>]
Order of execution
1. From
2. [Where] (if used) [ROW BY ROW]
3. Select
INTERSECT
Intersect operator is used to combine two select statements ,it returns only common value [equal
value] present in the table[column_name].
SYNTAX:
SELECT COLUMN_NAME
FROM TABLE_NAME
[WHERE <CONDITION>]
INTERSECT
SELECT COLUMN_NAME
FROM TABLE_NAME
[WHERE <CONDITION>];
ORDER OF EXECUTION:
1)FROM
2)[WHERE]
3)SELECT
Note :
MINUS
MINUS operator is used to compare to select statements, it returns only the row that are present in the
first statement , but are not in second statement ,[distinct value]. MINUS elements the duplicate rows.
SYNTAX:
SELECT COLUMN_NAME
FROM TABLE_NAME1
[WHERE <CONDITION>]
MINUS
SELECT COLUMN_NAME
FROM TABLE_NAME2
[WHERE <CONDITION>];
ORDER OF EXECUTION:
1)FROM
2)[WHERE]
New Section 1 Page 114
2)[WHERE]
3)SELECT
Note :
Escape character
Escape character is used to remove the special behavior of the special character and treated as normal
character (character).
Note:
1)Escape character must be defined.
2)Escape character must be written before the special character which has to be treated as normal
character.
3)The recommended character for the escape character
Syntax;
EXAMPLE:
1)WAQTD names of the employee if the names starts with an underscore.
SELECT ENAME
FROM EMP
WHERE ENAME LIKE '%!_%' ESCAPE '!';
2)WAQTD name of the employee having % as the first character and underscore has the 4th character.
SELECT ENAME
FROM EMP
WHERE ENAME LIKE '% ! %_ _ _%' ESCAPE '!' ;
SELECT ENAME
FROM EMP
WHERE ENAME LIKE '% ! %' OR ENAME LIKE '% ! _' ESCAPE '!' ;
PSEUDO COLUMN
Pseudo columns are the false column that are present in each and every table and must be called
explicitly.
We have 2 types:
1) ROWID
2) ROWNUM.
1) ROWID
Note:
SYNTAX:
SELECT */COLUMN_NAME/EXPRESSION
FROM EMP E1
WHERE ( SELECT COUNT([ DISTINCT] ROWID )
FROM EMP E2
WHERE E1.ROWID >= E2.ROWID) = N ;
SELECT */COLUMN_NAME/EXPRESSION
FROM EMP E1
WHERE ( SELECT COUNT([ DISTINCT] ROWID )
FROM EMP E2
WHERE E1.ROWID< =E2.ROWID) = N ;
EXAMPLE:
SELECT ROWID,EMP.*
FROM EMP;
ASSIGNMENT ON ROWID:
2) ROWNUM
Note:
Example:
1) WAQTD ROWNUM ALONG WITH ALL THE DETAILS OF EMP.
EMP
ENAME SAL
A 2000
B 1000
C 3000
D 2500
E 4000
SELECT *
FROM EMP
New Section 1 Page 118
FROM EMP
WHERE ROWNUM<4;
Example:
EMP
ENAME SAL
A 2000
B 1000
C 3000
D 2500
E 4000
SL NO ENAME SAL
1 A 2000
2 B 1000
3 C 3000
4 D 2500
5 E 4000
SELECT *
FROM (SELECT ROWNUM SLNO,EMP.*
FROM EMP)
WHERE SLNO=3;
EXAMPLE: