Structured Query Language (SQL) .: What Is DATA?
Structured Query Language (SQL) .: What Is DATA?
If we want to communicate with the database through any JAVA application then we need a query language
known as, STRUCTURED QUERY LANGUAGE (SQL).
What is DATA?
What is DATABASE?
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
4. L O B (Large Object)
5. NUMBER
DQL
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;
5. DISTINCT:-
The distinct clause is used to remove the duplicate values of a given column
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 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 :-
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.
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 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%';
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')
-----------------
2 WHERE DEPTNO=10; 5
5
UPPER :- 4
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
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.
EXAMPLE SELECT REPLACE ('QSPIDER', 'Q', 'J') FROM DUAL; (Q will be replaced with J)
SELECT REPLACE ('QSPIDER', 'S') FROM DUAL; (S will be omitted)
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)
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;
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');
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)
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)