Structured Query Language
Structured Query Language
Dis-Advantages
It occupies more memory space as it stores the Data in the form of files.
Hence, the time taken for execution is more.
RDBMS: [Relational Database Management System]
Relational Model
It states that Data should be stored in the form of Tables.
This theory was proposed by Edger Frank CODD.
If the DBMS follows the Relational Model, we call it Relational Database
Management System [RDBMS].
i.e.; DBMS Relational Model RDBMS
To store the Data in the form of Tables we have to follow certain rules,
Rule 1: In a single cell we must store only single-valued Data.
Note: If we try to store multiple values in a single cell then there is a possibility of Data loss.
Rule 2: We can store Data in multiple Tables and we can establish a connection between
the Tables with the help of Key Attributes.
EMPLOYEE TABLE
EMP - NO ENAME DEP - NO DNAME LOC
101 A 10 X XYZ
102 B 20 Y MNO
103 C 10 X XYZ
104 D 10 X MNO
Rule 3: Data should be stored in the form of Tables including the MetaData in MetaTable.
#MetaData – The Data about the Data is known as MetaData.
#MetaTable – The Table that stores the MetaData is known as MetaTable and automatically
generates it.
EMP TABLE
METADATA
EMPNO ENAME PHOTO
SIZE – 100KB
101 A DATA PHOTO RESOLUTION – 4 X 25
102 B FORMAT - JPEG
METATABLE
SIZE RESOLUTION FORMAT
100 4 X 25 JPEG
120 4 X 50 PNG
Note: Assigning Data Types is mandatory but Constraints are optional and these are
assigned to the Columns.
Data Types
Here size represents the maximum no. of characters that we have to store.
The size value ranges from 1 to 2000.
VARCHAR: It is the same as char; the only difference is that it uses Variable Length Memory
Allocation instead of Fixed Length Memory Allocation. Its size also ranges from 1 to 2000.
Syntax: VARCHAR (SIZE)
Note: The input Data length should always be less than or equal to the size allotted.
i.e.; NAME CHAR (8) Input is PYSPIDERS whose length is 9
9 <= 8 = false # Not accepted / Error
VARCHAR2: It is an updated version of VARCHAR with a storage capacity of 1 to 4000.
It is used automatically when the size exceeds the 2000 limit.
NUMBER: It can store only the Numbers.
Syntax: NUMBER (PRECISION, [SCALE])
Required Not Mandatory
Precision: It represents the maximum no. of digits to be allocated.
Scale: It represents the no. of decimal values within the Precision.
Note: The default Scale value is zero ‘0’.
e.g.;
1. NUMBER (5) - ± _ _ _ _ _ [Normal Syntax].
2. NUMBER (5, 0) - Same Meaning as 1.
3. NUMBER (5, 2) -±___.__
4. NUMBER (5, 7) - ± . 00 _ _ _ _ _
5. NUMBER (5, 5) -±._____
DATE: It can store only the Date.
Syntax: DATE
Oracle DATE Formats
‘DD – MON – YYYY’ ‘11 – APR – 2023’
‘DD – MON – YY’ ‘11 – APR – 23’
LARGE OBJECTS: It is used to store Data up to 4 GB.
LARGE OBJECTS are of two types,
1st) CHARACTER LARGE OBJECTS: It is used to store characters with up to 4 GB
of memory.
Syntax: CLOB
2nd) BINARY LARGE OBJECTS: It is used to store Binary values of Images, Audio,
Videos, Pdfs, etc. up to 4 GB.
Syntax: BLOB
Constraints (CONDITIONS)
These are the conditions given to the columns for validation.
Constraints are of five types,
1. UNIQUE
2. NOT NULL
3. CHECK
4. PRIMARY KEY
5. FOREIGN KEY
UNIQUE: It doesn’t accept duplicated or repeated values.
Syntax: UNIQUE
NOT NULL: It doesn’t accept null. It is used where entering Data is mandatory.
Syntax: NOT NULL
Note:
NULL is a keyword that represents an empty cell.
NULL doesn’t represent zero ‘0’ or space ‘ ’.
We can’t perform any arithmetic operations on NULL, because the output will always
be NULL itself.
e.g.; NULL * 2 = NULL
We can’t identify an empty cell by equating it with the NULL Keyword, because the
output will always be false. So, to identify NULL cells we use Identity Operator.
It deals with the structure of the Table or Object present in the Database.
It has five Statements,
1. CREATE
2. RENAME
3. TRUNCATE
4. ALTER
5. DROP
Important Point:
SELECT COLUMN_NAME, A.CONSTRAINT_NAME, CONSTRAINT_TYPE, SEARCH_CONDITION
ALTER: It is used to Modify / Alter the Structure of the Table in five ways,
We can’t modify the column’s Data Type, which has CHECK Constraints.
e.g.; Query: ALERT TABLE STUDENT
MODIFY PH_NO VARCHAR (10);
Error: Cannot Modify Column Data Type with current Constraint.
We can’t modify the column’s Data Type, which is FOREIGN KEY Constraints.
e.g.; Query: ALERT TABLE STUDENT
MODIFY STREAM CHAR (10);
Error: Column Type incompatible with Reference Column type.
We can’t modify the Data Type of the column if the Column is PRIMARY KEY and is
referenced to another table.
e.g.; Query: ALERT TABLE BRANCH
MODIFY BNAME CHAR (10);
Error: Column Type incompatible with Reference Column type.
We can modify the column’s Data Type in the rest of the cases.
Modification of the Structure of the Table is not easy when the Table has data
within it.
FLASHBACK: It is used to recover the Table from the Recycle Bin back to the Database
where it was present previously.
Syntax: FLASHBACK TABLE TABLE_NAME
TO BEFORE DROP;
PURGE: It is used to Delete the Table from the Recycle Bin completely.
Syntax: PURGE TABLE TABLE_NAME;
Note: We use the following command to display the table in the Recycle Bin.
T1 T1
DROP PURGE
BIN$ABC BIN$ABC
FLASHBACK
It is used to manipulate the table by Inserting, Updating, or Deleting the records from
the table.
It has three Statements,
INSERT
UPDATE
DELETE
INSERT ALL
INTO TABLE_NAME (COL1, COL2, ……..., COLn) VALUES (VAL1,
VAL2, ……..., VALn)
INTO TABLE_NAME (COL1, COL2, ……..., COLn) VALUES (VAL1,
VAL2, ……..., VALn)
INTO TABLE_NAME (COL1, COL2, ……..., COLn) VALUES (VAL1,
VAL2, ……..., VALn)
SELECT * FROM DUAL;
e.g.; INSERT ALL
INTO BRANCH (BNAME, HOD, BLOCK) VALUES('ECE', 'RITA', 'A')
INTO BRANCH (BNAME, HOD, BLOCK) VALUES('CSE', 'MONICA', 'B')
INTO BRANCH (BNAME, HOD, BLOCK) VALUES('MECH', 'AKSHAYA', 'C')
INTO BRANCH (BNAME, HOD, BLOCK) VALUES('MCA', 'QUEEN', 'D')
SELECT 1 FROM DUAL;
Note:
Syntax: COMMIT;
INSERT INSERT
e.g.;
UPDATE UPDATE
INSERT INSERT
COMMIT
INSERT
DELETE
Worksheet Database
ROLBACK: It is used to undo the Transactions after COMMIT or the previously saved
Transactions.
Syntax: ROLLBACK;
e.g.;
INSERT INSERT
UPDATE UPDATE
INSERT INSERT
COMMIT
INSERT
UPDATE
DELETE
ROLLBACK
Worksheet Database
SAVEPOINT: It is used to mark the Positions or Restoration points.
Syntax: SAVEPOINT SAVEPOINT_NAME;
Worksheet Database
PROJECTION
Syntax: SELECT * / [DISTINCT] COLUMN_NAME / EXPRESSION [ALIAS]
FROM TABLE_NAME;
Order of Execution
FROM – First the from Statement will execute.
SELECT – It will execute after from Statement.
Working Principle
1st) The FROM Clause will be executed first after then the controller will fetch the Table from
the Database and put it under execution.
2nd) To the FROM Clause we can pass TABLE_NAME as an Argument.
3rd) After the execution of the FROM Clause, the SELECT Clause will be executed.
4th) To the SELECT Clause we can pass three Arguments, “ * ” Asterisk, COLUMN_NAME,
EXPRESSION.
th
5 ) The job of the SELECT Clause is to create the result table.
6th) “ * ” Asterisk represents Select the records present in all the Columns.
7th) Along with the “ * ” Asterisk, we can’t give any COLUMN_NAME or EXPRESSION as an
argument to the SELECT Clause.
ST (Student Table)
SID SNAME PER BNAME
1 NIKHIL 84 MCA
2 SATYA 32 BCOM
3 SUBBU 70 MECH
4 GOKUL 84 MCA
5 SRUJANA 65 MECH
6 MANJU 70 MCA
Question-1) Write a Query to Display Student Name [SNAME] and Branch Name
[BNAME] present in the Student Table [ST].
Question-3) Write a Query to Display the Student Name [SNAME], Percentage [PER],
Student ID [SID], and Branch Name [BNAME].
Answers) SELECT SNAME, PER, SID, BNAME
FROM ST;
Question-4) Write a Query to Display Student ID [SID] along with Branch Name [BNAME].
Answers) SELECT SID, BNAME
FROM ST;
DISTINCT
It is used to delete Repeated Data.
DISTINCT must be the first Argument in the SELECT Clause.
We can pass more than one Column name.
It removes the combination of Duplicate values.
BNAME RESULT
MCA BNAME
BCOM MCA
MECH BCOM
MCA MECH
MECH
MCA
Question-6) Write a Query to Display DISTINCT Percentage [PER] and Branch Name
[BNAME].
Answers) SELECT DISTINCT PER, BNAME
FROM ST;
RESULT
PER BNAME
84 MCA PER BNAME
32 BCOM 84 MCA
70 MECH 32 BCOM
84 MCA 70 MECH
65 MECH 65 MECH
70 MCA 70 MCA
EXPRESSION
It is a Mathematical Statement that gives Results.
It is always the combination of Operators and Operands (Literals).
An Expression must contain two Operands and a single Operator.
Literals: They are the actual values given to perform a Task. Literals are of three types,
ALIAS
Note:
If we ever use ( * ) Asterisk alone without the Table_Name then the Controller will
expect the FROM keyword as soon as we give ( * ) Asterisk.
Important Point
For Single Line Comment we use: - - (Double Dash)
For Multiple Line Comments we use: /* …………………...
………………...……
*/
Question-1) Write a Query to Display Employee Name (ENAME) with Annual Salary for all
Employees.
Answers) SELECT ENAME, SAL * 12 “ANNUAL SALARY”
FROM EMP;
Question-2) Write a Query to Display Employee Name (ENAME), Job, Salary (SAL) with
rs.100 Bonus.
Question-3) Write a Query to Display Employee Name (ENAME), Job, and Annual Salary
with 30 % Hike.
Question-5) Write a Query to Display Employee Name (ENAME), Annual Salary with 15 %
Deduction.
Question-6) Write a Query to Display Salary (SAL), Half Term Salary, and Annual Salary
SELECTION
It is the process of retrieving the Data by selecting both Rows and Columns.
1st) WHERE
Order of Execution
Question-1) Write a Query to Display Employee Name (ENAME) who are working in
Department no. - 20.
WHERE
Result Table
SE
L
EC
T
O
/P
Question-2) Write a Query to Display Employee Name (ENAME) and Job who are working
as Manager.
Question-3) Write a Query to Display Employee Name (ENAME) and HIREDATE who
were hired after 25th-Apr-1982.
Question-4) Write a Query to Display Employee Name (ENAME) and HIREDATE who
were Hired before 1982.
Question-5) Write a Query to Display Employee Name (ENAME) who were working as
Clerk.
Answers) SELECT ENAME
FROM EMP
WHERE JOB = ‘CLERK’;
Question-6) Write a Query to Display Employee Name (ENAME) and Salary who were
working as Salesman.
Answers) SELECT ENAME, SAL
FROM EMP
WHERE JOB = ‘SALESMAN’;
Question-7) Write a Query to Display the Details of the Employees who were working
as Salesman.
Answers) SELECT *
FROM EMP
WHERE JOB = ‘SALESMAN’;
Question-8) Write a Query to Display the Details of the Employees who were name is
JONES.
Answers) SELECT *
FROM EMP
WHERE ENAME = ‘JONES’;
Question-9) Write a Query to Display the Details of the Employees who were Hired
after 1st-Jan-1981.
Answers) SELECT *
FROM EMP
WHERE HIREDATE = ‘01-JAN-1981’;
Question-10) Write a Query to Display the Employee Name (ENAME), SAL, and Annual
Salary if Annual Salary is more than 12,000.
Answers) SELECT ENAME, SAL, SAL * 12 “Annual Salary”
FROM EMP
WHERE SAL * 12 > 12000;
Question-11) Write a Query to Display the Details of the Employees who were name is
JONES.
Answers) SELECT *
FROM EMP
WHERE ENAME = ‘JONES’;
Question-12) Write a Query to Display the Details of the Employees who were having
Commission more than Salary.
Answers) SELECT *
FROM EMP
WHERE COMM > SAL;
Question-13) Write a Query to Display the Details of the Employees who were earning
more than 2,000 per month.
Answers) SELECT *
FROM EMP
WHERE SAL > 2000;
Question-14) Write a Query to Display the Employee Name (ENAME) and SAL who’s
Commission is 1,400.
2nd) OPERATORS
Concatenation Operators
Question-1) Write a Query to Display Employee Name (ENAME) with suffix “HELLO”.
Question-2) Write a Query to Display Employee Name (ENAME) along with Job in the
given format “(ENAME) is working as (JOB)”
AND: It is used to select the Record if all the Conditions are True.
Truth Table
Condition 1 Condition 2 Result
WHERE
SELECT O/P
Result Table
Question-2) Write a Query to Display Employee Name (ENAME) earning Salary more
than 1000 and working as a CLERK.
Question-3) Write a Query to Display Employee’s Details who are not working in
Department no. 10 and Department no. 20.
Question-4) Write a Query to Display Employee Name (ENAME) who are Hired after the
Year 81 in Department no. 10.
Question-5) Write a Query to Display Employee Name (ENAME), SAL, and Annual Salary
who’s SAL > 1500 and Annual Salary > 15000.
Question-6) Write a Query to Display Employee’s Details Hired in the Year 1981.
Answers) SELECT *
FROM EMP
WHERE HIREDATE >= ’01-JAN-81’ AND HIREDATE <= ’31-DEC-81’;
Question-7) Write a Query to Display Employee Name (ENAME) and Salary whose SAL is
in range of 1000 and 3000.
Result Table
WHERE
SELECT O/P
Question-2) Write a Query to Display the Name of the Employee who were Working as a
CLERK or MANAGER.
Question-3) Write a Query to Display the DEPTNO, EMPNO, and JOB if the Employee
reports to 7902 and 7839.
Question-4) Write a Query to Display the Details of the Employee who were working in
DEPTNO = 20 or working as SALESMAN.
Answers) SELECT *
FROM EMP
WHERE DEPTNO = 20 OR JOB = ‘SALESMAN’;
Question-5) Write a Query to Display the ENAME, DEPTNO, and JOB of the Employee
who is working in DEPTNO = 10 or 20 as a CLERK.
Answers) SELECT *
FROM EMP
WHERE (JOB = ‘CLERK’ OR JOB = ‘MANAGER’) AND (DEPTNO =
20 OR DEPTNO = 30);
Question-7) Write a Query to Display the Details of the Employee who were not working
as MANAGER and also not working in DEPTNO =20 or 30.
Answers) SELECT *
FROM EMP
WHERE JOB != ‘MANAGER’ AND (DEPTNO = 20 OR DEPTNO =30);
Question-8) Write a Query to Display the Details of the Employee who are not hired in
Year – 81 and working in either DEPTNO =10 or 30.
Answers) SELECT *
FROM EMP
WHERE (HIREDATE < ’01-JAN-81’ OR HIREDATE > ’31-DEC-81’)
AND (DEPTNO = 10 OR DEPTNO = 30);
Question-1) Write a Query to Display the Employee Name (ENAME) who isn’t working in
DEPTNO - 20.
3.WAQTD DETAILS OF THE EMP ALONG WITH ANNUAL SALARY IF THEY ARE
WORKING IN DEPT 30 AS SALESMAN AND THEIR ANNUAL SALARY HAS TO BE
GREATER THAN 14000.
5.WAQTD NAMES OF THE EMPMLOYEES WHOS SALARY IS LESS THAN 1100 AND
THEIR DESIGNATION IS CLERK.
6.WAQTD NAME AND SAL, ANNUAL SAL AND DEPTNO IF DEPTNO IS 20 EARNING
MORE THAN 1100 AND ANNUAL SALARY EXCEEDS 12000.
20.WAQTD ALL THE DETAILS ALONG WITH ANNUAL SALARY IF SAL IS BETWEEN
1000 AND 4000 ANNUAL SALARY MORE THAN 15000.
Special Operators
Question-1) Write a Query to Display the name of the Employee who are working as a
CLERK, SALESMAN or as an ANALYSLT.
Question-2) Write a Query to Display the Details of the Employee who are working as a
SALESMAN or as an ANALYSLT in DEPTNO – 20 or 30.
Answers) SELECT *
FROM EMP
WHERE JOB IN (‘SALESMAN’, ‘ANALYST’) AND DEPTNO (20, 30);
Question-3) Write a Query to Display the Details of the Employee who’s is either KING,
BLAKE OR SMITH and working in DEPTNO – 10 or 30.
Answers) SELECT *
FROM EMP
WHERE ENAME IN (‘KING’, ‘BLAKE’, ‘SMITH’) AND DEPTNO (10, 30);
Question-1) Write a Query to Display the Details of the Employee who are not working
in DEPTNO – 10 or 30.
Answers) SELECT *
FROM EMP
WHERE DEPTNO NOT IN (10, 30);
Question-2) Write a Query to Display the Details of the Employee along with Annual
Salary if Employee doesn’t work as PRESIDENT or as SALESMAN AND
Annual Salary more than 14000.
Question-2) Write a Query to Display the Details of the Employee who are hired in year 81
in DEPTNO – 20 or 30.
Answers) SELECT *
FROM EMP
WHERE HIREDATE BETWEEN (’01-JAN-81’ AND ’31-DEC-81’) AND
DEPTNO IN (20, 30);
Question-3) Write a Query to Display the Details of the Employee who are earning more
than 1000 but less than 3000.
Answers) SELECT *
FROM EMP
WHERE SAL BETWEEN 1001 AND 2999;
Question-4) Write a Query to Display the Details of the Employee who are hired after year
81 but before year 87 and having Annual Salary in range of 10000 to 15000.
Answers) SELECT *
FROM EMP
WHERE HIREDATE BETWEEN (’01-JAN-81’ AND ’31-DEC-86’) AND
SAL*12 BETWEEN 10000 AND 15000;
NOT BETWEEN: It is opposite of between.
It selects the record if it is not between the given range.