Structured Query Language (SQL) .: What Is DATA?

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

SQL

If we want to communicate with the database through any JAVA application then we need a query language
known as, STRUCTURED QUERY LANGUAGE (SQL).

There are 5 pillars of SQL,


1. DQL (Data Query Language)
2. DDL (Data Definition Language)
3. DML (Data Manipulation Language)
4. TCL (Transaction Control Language)
5. DCL (Data Control Language)

What is DATA?

 Data is the raw facts which describes the properties of an entity.

What is DATABASE?

 Database is a place / medium in which we can store our Data.


 By using DATABASE we can perform the C R U D operation.
o C  Create / Insert
o R  Read / Retrieve
o U  Update / Modify
o D  Delete / Drop

What is DBMS (Database Management System)?

 The DBMS Stands for Database Management System.


 It is used to maintain and manage the Database.
 The main advantages of DBMS are Security and Authorization.
 We need a Query Language to communicate with DBMS.
 In DBMS the data will be stored in the form of files.
What is RDBMS (Relational Database Management System)?

 RDBMS stands for Relational Database Management System.


 In which the data will be stored in the form of tables.
 The table is a combination of multiple Rows and Columns.
 The main advantages of RDBMS are Security and Authorization.
 Any DBMS software that follows the Relational model will become RDBMS.
 The relational model was developed by Scientist E.F. Codd in which the data will be stored in the
form of a table.
 The data entered into the cell must be Single Valued Data (Means we can add only one data to a
single cell. For EXAMPLE- We can write Age-22 but we can not write Age-21,22)

What is Data Type?

 The data type in SQL tells us which type of data we can store in a particular Column.
 There are 5 types of Data Types.

1. CHAR
2. VARCHAR
VARCHAR 2
3. DATA
4. L O B (Large Object)
i. C L O B (Character Large Object)
ii. B L O B (Binary Large Object)
5. NUMBER

1. CHAR

 The allowed characters are A-Z (uppercase), a-z (lowercase), 0-9 (Numbers), and Special Characters.
 At the time of declaration of CHAR data type, it is mandatory to mention the size.
 It is mandatory to use single quote (‘a’) with CHAR type value.
 The maximum size of CHAR data type is 2000.
 The CHAR data type follows Fixed Length Memory Allocation.

2. VARCHAR

 It is similar to CHAR data type except the following differences like, the VARCHAR data type follows
variable length memory allocation.
VARCHAR 2
 It is updated version of VARCHAR.
 In VARCHAR 2 we can store the characters up to 4000.

3. DATE

 Syntax :- DD-Mon-YYYY -> 28-Mar-2024


DD-Mon-YY -> 28-Mar-24

4. L O B (Large Object)

 There are 2 types of Large Object,


i. Character Large Object (C L O B)
ii. Binary Large Object (B L O B)

5. NUMBER

 Syntax :- i) NUMBER (Precision, Scale)


EX: NUMBER (10, 4) => 784894.0650 (10, 4)
ii) NUMBER (Size)

DQL

 If we want to retrieve data from database we should go for D Q L.

1. SELECT –
If we want to retrieve the data and display it with in the Console then we use select Clause.
2. PROJECTION –
It is used to retrieve the data from a given table by selecting only columns.
3. SELECTION –
If we want to retrieve the data from a given table by selecting both column and rows then we
should go for Selection.
Q. Write a query to display the details of the employees working in department number 20 ?
SELECT * FROM EMP WHERE DEPTNO = 20;
Q. Write a query to display the name, job and department number of the employee working in
department number 30 ?
SELECT ENAME, JOB, DEPTNO FROM EMP WHERE DEPTNO = 30;
Q. Write a query to display the name, job and salary of the employee those are working as salesman ?
SELECT ENAME, JOB, SAL FROM EMP WHERE JOB = ‘SALESMAN’;
Q. Write a query to display the name and salary of the employees those are earning more than
1500 ?
SELECT ENAME, SAL FROM EMP WHERE SAL>1500;

4. JOIN –
It is used to retrieve the data by using multiple table simultaneously.
Q. Write a query to display the name of the employees ?
SELECT ENAME FROM EMP;
Q. How can we display the tables , present In the database?
SELECT * FROM TAB;
Q. Write a query to display the details of the employee?
SELECT * FROM EMP;
Q. Write a query to display the salary of the employees?
SELECT SAL FROM EMP;
Q. Write a query to display the data of the employee?
SELECT HIREDATA FROM EMP;
Q. Write a query to display the name and salary of the employee?
SELECT ENAME,SAL FROM EMP;

Q. Write a query to display the details of the employees working as analyst?


SELECT * FROM EMP WHERE JOB = ‘SALESMAN’;
Q. Write a query to display the job of the employee?
SELECT JOB FROM EMP;

5. DISTINCT:-

The distinct clause is used to remove the duplicate values of a given column

Q. Write a query to display the job of the employees by removing duplicates?


SELECT DISTINCT JOB FROM EMP;
Q. Write a query to display the department number of the employees by removing duplicate?
SELECT DISTINCT DEPTNO FROM EMP;
Q. Write a query to display the job and department number of the employees by removing duplicates?
SELECT DISTINCT JOB, DEPTNO FROM EMP;

 We can use multiple column as an argument to DISTINCT clause, or it will remove the combination
of columns in which the values are duplicated .

Q. Write a query to display the annual salary of the employees?


SELECT SAL * 12 FROM EMP;

Q. Write a query to display the salary of the employees with a hike of 20%?
select sal + (20/100) * sal from emp;
select sal * 1.2 from emp;
Q. Write a query to display the name and salary of the employees with a deduction of 50%?
select ename, sal/2 from emp;
select ename, sal-(50/100)*sal from emp;

6. OPERATOR :-

i. CONCATINATION OPERATOR (||)


This operator is used to merge two strings
Example-- select 'Hii '||ename from emp;
select 'Hii' || ename || 'How are you' from emp;
select 'Hii' || ename from emp (Press enter)
2 where ename = 'SCOTT';
LOGICAL OPERATOR :-
There are 2 types of Logical operators such as AND & OR.
The AND Operator returns TRUE if both the conditions are TRUE. Remaining all the cases, it returns false.
The OR Operator returns TRUE if any one of the conditions is TRUE. The remaining cases are false.

Q. Write a query to display the job, dept no. of the employees working as manager in department
number 10?
select job, deptno from emp
2 where job='MANAGER' AND deptno=10;
Q. Write a query to display the name, dept no. and job of the employees working as salesman in dept
no. 30?
Select job,deptno from emp
2 where job=’SALESMAN’ AND deptno=30;

Q. Write a query to display the name, dept no. and salary of the employees working in dept no. 20 and
earning salary more than 2200?
SELECT JOB, DEPTNO, SAL FROM EMP
2 WHERE DEPTNO=20 AND SAL>2200;

Q. Write a query to display the name and salary of the employees those are earning more than 1500 but
less than 3000?
SELECT ENAME, SAL FROM EMP
SELECT ENAME, SAL FROM EMP
Q. Write a query to display the name and dept no. of the employees working in dept no. 10 or 30?
SELECT ENAME, DEPTNO FROM EMP
2 WHERE DEPTNO=10 OR DEPTNO=30;
Q. Write a query to display the name, salary and dept no. of the employees earning more than 2000 and
less than 3000 in dept no. 20?
SELECT ENAME, SAL, DEPTNO FROM EMP
2 WHERE (SAL>2000 AND SAL<3000) AND DEPTNO=20;
Q. Write a query to display
SPECIAL OPERATOR
1. In Operator :-
 It is the multi valued operator which can accept multiple values at RHS.

Select * from emp


2 where deptno in (10,20)

Q. Write a query to display the name and job of the employee working as clerk or salesman?
SELECT * FROM EMP
2 WHERE JOB IN ('CLERK','SALESMAN');

2. NOT IN operator :-
 It is the opposite of IN operator.

Q. Write a query to display the name and dept no. of the employees those are not working in dept no.
(20,30)?
SELECT ENAME,DEPTNO FROM EMP
2 WHERE DEPTNO NOT IN(20,30);

3. BETWEEN Operator :-
 It is used to select the values on a given range.

Q. Write a query to display the salary between 1250 to 3000?


SELECT SAL FROM EMP
2 where sal between 1250 AND 3000;
Q. Write a query to display the name and hire date of the employees those are hired in the year of
1981?
SELECT ENAME,HIREDATE FROM EMP
2 WHERE HIREDATE BETWEEN '01-JAN-1981' AND '31-DEC-1981';
Q. Write a query to display the details of the employees those are hired in the month of April and
earning more than 2500?
SELECT * FROM EMP
2 WHERE (SAL > 2500) AND ( HIREDATE BETWEEN '01-APR-1987' AND '30-APR-1987');
4. NOT BETWEEN Operator :-
It is the opposite of BETWEEN Operator .
Q. Write a query to display the name and hire date of the employees those are hired after 1981 and
before 1983?
SELECT ENAME,HIREDATE FROM EMP
2 WHERE HIREDATE > '31-DEC-1981' AND HIREDATE < '01-JAN-1983';
OR
SELECT ENAME,HIREDATE FROM EMP
2 WHERE HIREDATE BETWEEN '31-DEC-1981' AND '01-JAN-1983';

Q. write a query to display the name of the employees along with annual salary working as clerk and
manager in dept no. 10 or 30 and annual salary > 22000?
SELECT ENAME,DEPTNO,(SAL*12) AS ANNUAL_SAL FROM EMP
2 WHERE (JOB = 'CLERK' OR JOB='MANAGER') AND DEPTNO IN (10,30) AND (SAL*12)>22000;
Q. Write a query to display the details of the employees along with their annual salary?
SELECT EMP.*,SAL*12 FROM EMP;

5. IS Operator :-
It is used to compare the null values.
Q. Write a query to display the details of the employees those are not getting commission ?
SELECT * FROM EMP
2 WHERE COMM IS NULL;

6. IS NOT Operator :-
Write a query to display the details of the employees those are getting commission.?
SELECT * FROM EMP
2 WHERE COMM IS NOT NULL;

7. LIKE Operator :-
This operator to catch the pattern in SQL.
Q. Write a query to display the name of the employees contains ‘A’ as their first character ?
SELECT ENAME FROM EMP
2 WHERE ENAME LIKE 'A%';
Q. Write A query to display the details of the employees contains ‘S’ as their last character?
SELECT ENAME FROM EMP
2 WHERE ENAME LIKE '%S';

Q. Write a query to display the details of the employees along with their annual salary contains ‘M’
as their first character and ‘N’ as their last character?
SELECT EMP.*, SAL*12 FROM EMP
WHERE ENAME LIKE 'M%N';
Q. Write a query to display the name of the employees those contains 2 times ‘A’ in their name?
SELECT ENAME FROM EMP
2 WHERE ENAME LIKE '%A%A%';
Q. Write a query to display the details of the employees contains 2 consecutive L (‘LL’) in their
names?
SELECT ENAME FROM EMP
2 HERE ENAME LIKE '%LL%';
Q. Write a query to display the job of the employees containing ‘S’ as their second last character?
SELECT JOB FROM EMP
2 WHERE JOB LIKE '%S_';
Q. Write a query to display the name of the employees containing ‘E’ as their second last character ?
SELECT ENAME FROM EMP
2 WHERE ENAME LIKE '%E_';
Q. Write a query to display the name and dept no. of the employees containing ‘U’ as the second
character ?
SELECT ENAME,DEPTNO FROM EMP
2 WHERE ENAME LIKE '_U%';
Q. Write a query to display the hire date of the employees those are hired in the month of April?
SELECT HIREDATE FROM EMP
2 WHERE HIREDATE LIKE '%APR%';

8. NOT LIKE Operator :-


Q. Write a query to perform the addition of two numbers by using SQL?
SELECT 2+3 FROM DUAL; 2+3  THIS IS WRONG
----
5

DUAL :-
 Dual is the dummy table present inside each and every database to perform any random
operations.
 The dual table contains only one column and one row.

ALIAS :-
 It is used to provide the alternative name of s given column.
 We can achieve ALIAS with or without using ‘AS’ keyword.
Q. Write a query to perform the addition of two numbers by using SQL?
SELECT 2+3 AS "SUM" FROM DUAL; SUM  This is right
-----
5
Q. Write a query to show the annual salary of employees?
 SELECT SAL*12 AS "ANNUAL SAL" FROM EMP; (HERE “ “ IS MANDATORY.)
 SELECT SAL*12 AS ANNUAL_SAL FROM EMP; (IF NOT USING “ ” THE WE CAN USE _ (Under score)
AS WELL.)
 SELECT SAL*12 AS ANNUALSAL FROM EMP; (WITH OUT GIVING ANY SPACE OR “ ” OR _ WE CAN
WRITE IT AS WELL).

FUNCTION :-
 The function in SQL is used to perform some operations.
 There are 2 types of function –
1. Single row function and
2. Multi row function.
SINGLE ROW FUNCTION
LENGTH :-
It is used to count the number of characters present in a given string.
 SELECT LENGTH ('SUBRATA') FROM DUAL; LENGTH('SUBRATA')
-----------------

 SELECT LENGTH (ENAME) FROM EMP; LENGTH(ENAME)


 SELECT LENGTH (ENAME) FROM EMP
-------------

2 WHERE DEPTNO=10; 5

5
UPPER :- 4

It is used to convert a given string into fully uppercase character . 6

SELECT UPPER ('qspiders') from dual;

LOWER :-
It is used to convert string into a lower case character.
SELECT LOWER (‘QSPIDERS’) FROM DUAL;

INITCAP() :-
It is used to convert a string into initial uppercase character.
SELECT INITCAP ('queue') FROM DUAL;

REVERSE :-
It is used to reverse a string.
SELECT REVERSE (' GUA GUA GUE') FROM DUAL;
SUBSTR :-
It is used to retrieve the sub string of a given string.
SYNTAX  SUBSTR (‘org-str’, ‘p1’, ‘p2’);
Where  p1 = Starting point
P2 = Number of characters need to show

EX :- SELECT SUBSTR ('SUBHASHREE', 2,4) FROM DUAL; (o/p – UBHA)


SELECT SUBSTR ('QSPIDER',-5,3) FROM DUAL; (o/p – PID )
SELECT SUBSTR ('QSPIDER',-5,-3) FROM DUAL; (o/p – (SPACE) )

SELECT SUBSTR ('QSPIDER',4) FROM DUAL; (o/p – IDER ) (it will show output upto the end)

REPLACE :-
It is used to replace a part of a string into another string.

Syntax- REPLACE (‘ORGSTR’, ‘STR1’, ‘STR2’);

EXAMPLE  SELECT REPLACE ('QSPIDER', 'Q', 'J') FROM DUAL; (Q will be replaced with J)
SELECT REPLACE ('QSPIDER', 'S') FROM DUAL; (S will be omitted)

Q. Write a query to display the no. of times ‘A’ present in banana ?


SELECT (LENGTH ('BANANA') - LENGTH (REPLACE('BANANA','A'))) FROM DUAL;
SELECT (LENGTH ('BANANA') - LENGTH (REPLACE('BANANA','A'))) AS LENGTH FROM DUAL;

MOD :-
SELECT MOD (10,20) FROM DUAL; (ANS // 10)
SELECT MOD (20,10) FROM DUAL; (ANS // 0)

ROUND :-
SELECT ROUND(4.6) FROM DUAL; (ANS // 5)
SELECT ROUND (4.5)FROM DUAL; (ANS // 5)
SELECT ROUND (4.4)FROM DUAL; (ANS // 4)

TRUNC :-
It is similar to the round function but, it is always round up the given number into the lower value.
SELECT TRUNC (4.9) FROM DUAL;
Q. Write a query to display todays date ?
SELECT SYSDATE FROM DUAL; OR SELECT CURRENT_DATE FROM DUAL;
Q. Write a query to display the date and timezone ?
SELECT SYSTIMESTAMP FROM DUAL;
Q. Write a query to display the last date of given month ?
SELECT LAST_DAY (SYSDATE) FROM DUAL;
Q. Write a query to display the first half of any name ?
SELECT SUBSTR ('SUBRAT',1,LENGTH('SUBRAT')/2) FROM DUAL; (FIRST HALF)
SELECT SUBSTR('SUBRAT',LENGTH('SUBRAT')/2 + 1) FROM DUAL; (SECOND HALF)

MULTI ROW FUNCTION


 It is used to take multiple input at a time and it provides only a single output.
 There are 5 types of Multi Row functions 
i. MAX ()
ii. MIN ()
iii. AVG ()
iv. SUM ()
v. COUNT ()

 Multi row function can accept at most 1 argument.


 Along with Multi row function we can not display any other column name in select clause.
 The multi row function ignores NULL VALUES.
 We can not use Multi row function with WHERE clause .
 Count is the only Multi row Function which can accept ‘*’ as an argument.

Q. Write a query to display the maximum salary of the employees ?


SELECT MAX(SAL) FROM EMP;
Q. Write a query to display the minimum salary of the employees?
SELECT MIN(SAL) FROM EMP;
Q. Write a query to display the average salary of the employees ?
SELECT AVG(SAL) FROM EMP;
Q. Write a salary to display the total salary of the employees ?
SELECT SUM(SAL) FROM EMP;
Q. Write a query to count the total number of employees ?
SELECT COUNT (ENAME) AS EMPLOYEE_NUMBER FROM EMP;
SELECT COUNT (*) AS NUMBER_OF_EMPLOYEES FROM EMP;
Q. Write a query to display the total salary given to manager ?
SELECT SUM(SAL) FROM EMP WHERE JOB='MANAGER';
Q. Write a query to count total number of salesman ?
SELECT COUNT (*) FROM EMP WHERE JOB='SALESMAN';
Q. Write a query to display the no. of employees earning more than 1500 in dept no. 20 ?
SELECT COUNT (*) FROM EMP WHERE SAL>1500 AND DEPTNO=20;
Q. Write a query to display the minimum salary given to the employees working as clerk in dept no. 10
or 20 ?
SELECT MIN(SAL) FROM EMP WHERE JOB = ‘CLERK’ AND (DEPTNO=10 OR DEPTNO=20) ;
SELECT MIN(SAL) FROM EMP WHERE JOB = 'CLERK' AND DEPTNO IN(10,20);
Q. Write a query to display the total no. of employees having ‘E’ in their names ?
SELECT COUNT (*) FROM EMP WHERE ENAME LIKE '%E%'

GROUP BY CLAUSE :-
 It is used to create the groups.
 Order of execution :
FROM
WHERE
GROUP BY
SELECT
Q. Write a query to count the no. of employees working in each department ?
SELECT COUNT (*),DEPTNO FROM EMP
2 GROUP BY DEPTNO;

 The group by clause executes row by row.


 After the execution of Group By clause we will get the groups it means any clause that executes
after Group By clause must be execute in groups.
 The Group By clause can be used with or without where clause.
Q. Write a query to display the total no. of employees working in each dept. except dept. no. 30 ?
SELECT COUNT (*),DEPTNO FROM EMP
2 WHERE DEPTNO NOT LIKE 30
3 GROUP BY DEPTNO;
OR
SELECT COUNT (*),DEPTNO FROM EMP
2 WHERE DEPTNO NOT IN 30
3 GROUP BY DEPTNO;
OR
SELECT COUNT (*),DEPTNO FROM EMP
2 WHERE DEPTNO != 30
3 GROUP BY DEPTNO;
Q. Write a query to display the no. of employees working in each job ?
SELECT COUNT (*),JOB FROM EMP
2 GROUP BY JOB;
Q. Write a query to display the total no. of employees working in each job having character ‘A’ in their
names ?
SELECT COUNT (*),JOB FROM EMP
WHERE ENAME LIKE '%A%'
GROUP BY JOB;
OR
SELECT COUNT (*),ENAME,JOB FROM EMP
2 WHERE ENAME LIKE '%A%'
GROUP BY ENAME,JOB;
Q. Write a query to display the total no. of employees working as manager in each department ?
SELECT COUNT (*),DEPTNO FROM EMP
2 WHERE JOB = 'MANAGER’
3 GROUP BY DEPTNO;
Q. Write a query to display the maximum salary of each job ?
SELECT MAX(SAL),JOB FROM EMP
2 GROUP BY JOB;

HAVING CLAUSE :-
 The Having clause is used to filter the groups
 The Having clause executes after the Group By clause.
 We can use Multi Row Function with Having Clause and it executes Group by Group.
Q. Write a query to display the groups of the employees in which minimum 4 employees are working in
each dept. no. ?
SELECT COUNT (*), DEPTNO FROM EMP
2 GROUP BY DEPTNO
3 HAVING COUNT (*) >= 4;
 Order of execution :
FROM
WHERE
GROUP BY
HAVING CLAUSE
SELECT
Q. Write a query to display the salaries that are repeated ?
SELECT COUNT (*), SAL FROM EMP
2 GROUP BY SAL
3 HAVING COUNT (*) > 1;
Q. Write a query to display the no. of employees working in each dept. Having atleast 2 Employees
containing ‘A’ in their names ?
SELECT COUNT (*), DEPTNO FROM EMP
2 WHERE ENAME LIKE '%A%'
3 GROUP BY DEPTNO
4 HAVING COUNT (*) >=2 ;
Q. Write a query to display the job wise maximum salary if the maximum salary exceeds 2500 ?
SELECT JOB, MAX (SAL) FROM EMP
2 GROUP BY JOB
3 HAVING MAX(SAL) >= 2500;
Q. Write a query to display the no. of employees earning salary more than 1200 in each job and the total
salary needed to pay to the employees of each job must exceeds 4000?
SELECT COUNT (*), JOB, SUM(SAL) FROM EMP
2 WHERE SAL >= 1200
3 GROUP BY JOB
4 HAVING SUM(SAL)>=4000;
Q. Write a query to display the no. of employees working in each dept. except president ?
SELECT COUNT (*), DEPTNO FROM EMP
2 WHERE JOB NOT IN 'PRESIDENT'
3 GROUP BY DEPTNO;
Q. Write a query to display the no. of employees and avg. salary in each dept. if their earning salary
more than 2000 ?
SELECT COUNT (*), DEPTNO, AVG(SAL) FROM EMP
WHERE SAL > 2000
GROUP BY DEPTNO;
SUB QUERY
 The query written inside another query is known as sub query.
 Whenever we have unknown condition in our question then we should go for sub query.
 Every sub query have minimum 2 query like, outer query and inner query.
 The output of the inner query acts as an input to the outer query.
Q. Write a query to display the details of the employees those are working in dept. no. 20 ?
SELECT * FROM EMP
WHERE DEPTNO=20;
Q. Write a query to display the details of the employees those are working in dept. no. same as SMITH ?
SELECT * FROM EMP
2 WHERE DEPTNO = (SELECT DEPTNO FROM EMP WHERE ENAME='SMITH');
Q. Write a query to display the details of the employees earning more than ‘MILLER’ ?
SELECT * FROM EMP WHERE SAL >
(SELECT SAL FROM EMP WHERE ENAME = 'MILLER' );
Q. Write a query to display the name and hire date of the employees those are hired after ‘JONES’ ?
SELECT ENAME, HIREDATE FROM EMP WHERE HIREDATE>
2 (SELECT HIREDATE FROM EMP WHERE ENAME='JONES');
Q. Write a query to display the name, salary and dept. no. of the employees those are earning more
than 2000 and working is same dept. no. as ‘JAMES’ ?
SELECT ENAME,SAL,DEPTNO FROM EMP WHERE SAL > 2000
2 AND DEPTNO = (SELECT DEPTNO FROM EMP WHERE ENAME = 'JAMES');
Q. Write a query to display the name and salary of the employees those are earning more than ‘ALLEN’
but less than ‘KING’ ?
SELECT ENAME,SAL FROM EMP WHERE SAL >
2 (SELECT SAL FROM EMP WHERE ENAME = 'ALLEN') AND SAL < (SELECT SAL FROM EMP WHERE ENAME =
'KING') ;
Q. Write a query to display the name and hire date of the employees those are hired after ‘JONES’ and
contain ‘S’ as their last character ?
SELECT ENAME, HIREDATE FROM EMP WHERE HIREDATE>
2 (SELECT HIREDATE FROM EMP WHERE ENAME='JONES') AND ENAME LIKE '%S';
Q. Write a query to display the name and designation of the employees those are working in the same
designation as ‘CLARK’ ?
SELECT ENAME, JOB FROM EMP WHERE JOB =
2 (SELECT JOB FROM EMP WHERE ENAME='CLARK');
Q. Write a query to display the name of the employees working in same dept. no. as ‘JAMES’ and
earning salary more than ‘ADAMS’ and working in same job role as ‘MILLER’ and hired after ‘MARTIN’ ?
SELECT ENAME, DEPTNO, SAL, JOB FROM EMP WHERE DEPTNO =
2 (SELECT DEPTNO FROM EMP WHERE ENAME = 'JAMES') AND SAL>
3 (SELECT SAL FROM EMP WHERE ENAME='ADAMS') AND JOB=
4 (SELECT JOB FROM EMP WHERE ENAME='MILLER') AND HIREDATE>
5 (SELECT HIREDATE FROM EMP WHERE ENAME = 'MARTIN');

NESTED SUB QUERY


Q. Write a query to display the second maximum salary of the employees ?
SELECT MAX(SAL) AS SECOND_MAX_SAL FROM EMP
2 WHERE SAL < (SELECT MAX(SAL) FROM EMP);
Q. Write a query to display the third maximum salary of the employees ?
SELECT MAX(SAL) AS THIRD_MAX_SAL FROM EMP
2 WHERE SAL<(SELECT MAX(SAL) FROM EMP
3 WHERE SAL < (SELECT MAX(SAL) FROM EMP));

DDL
 There are 5 statements of DDL
1. CREATE
2. RENAME
3. DROP
4. ALTER
5. TRUNCATE

1. CREATE
 The CREATE command in SQL is used to construct a table or object in the database.
 Syntax :- CREATE TABLE TABLE-NAME
 (col-1 Data_type constraints,
Col-2 Data_type constraints,
……………………………………………..
……………………………………………..
……………………………………………..
Col-n Data_type constraints);
CREATE TABLE STD1
2 (SID NUMBER(5),
3 SNAME VARCHAR(10),
4 SAGE NUMBER(2));
Name Null? Type
----------------------------------------- -------- -------------
SID NUMBER(5)
SNAME VARCHAR2(10)
SAGE NUMBER(2)

CREATE TABLE STD2


2 (GENDER VARCHAR(10),
3 SID NUMBER(10));
Name Null? Type
----------------------------------------- -------- ------------
GENDER VARCHAR2(10)
SID NUMBER(10)

CREATE TABLE EMPLOYEE1


2 (ENAME VARCHAR(20) NOT NULL,
3 DARAMA NUMBER(5) NOT NULL);
Name Null? Type
----------------------------------------- -------- -------------
ENAME NOT NULL VARCHAR2(20)
DARAMA NOT NULL NUMBER(5)

2. RENAME
 It is used to change the name of the objects or table.
 Syntax :- RENAME TAB_NAME TO NEW_NAME;
RENAME EMPLOYEE1 TO EMP1;
Name Null? Type
----------------------------------------- -------- ------------
ENAME NOT NULL VARCHAR2(20)
DARAMA NOT NULL NUMBER(5)
3. DROP
 The DROP command is used to delete the table from the database.
 SYNTAX :- DROP TABLE TABLE_NAME;
Q. How to delete the table from database ?
DROP TABLE EMP1;
TNAME TABTYPE
------------------------------ -------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
STD2 TABLE
STD1 TABLE
BIN$oK6a50PMTVaOD0KA5ReRVQ==$0 TABLE (this table has been deleted but stored in bin)

Q. How to recover the table from bin folder?


FLASHBACK TABLE EMP1 TO BEFORE DROP;

 SYNTAX :- FLASHBACK TABLE TABLE_NAME TO BEFORE DROP;


Q. How to delete the table from bin folder ?
PURGE TABLE EMP1;

 SYNTAX :- PURGE TABLE TABLE_NAME;

You might also like