sql_Day2
sql_Day2
1. SELECT : "It is used to retrieve the data from the table and display it.
2. 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 column are by default selected .
3. SELECTION : "It is a process of retrieving the data by selecting both the columns and rows is known as
Selection " .
4. 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 column are by default selected
.
SYNTAX ( QUERY ):
NOTE :
SELECT * / [DISTINCT] Column_Name / Expression [ALIAS]
➢ FROM Clause starts the execution .
FROM Table_Name ; ➢ For FROM Clause we can pass Table_Name as an
argument .
ORDER OF EXECUTION ➢ The job of FROM Clause is to go to the Database
and search for the table and put the table under
1. FROM Clause execution .
2. SELECT Clause ( From and Select ---clause / statement/ keywords) ➢ SELECT Clause will execute after the execution of
FROM Clause
➢ For SELECT Clause we pass 3 arguments
Example : Write a query to display names of all the students . ⬥ *
⬥ Column_Name
DATABASE ⬥ Expression
SELECT SNAME ➢ The job of SELECT Clause is to go the table under
Student
FROM STUDENT ; execution and select the columns mentioned .
➢ SELECT Clause is responsible for preparing the
result table .
Output of FROM Clause
Output of SELECT Clause ➢ Asterisk(*) : it means to select all the columns from
Student the table .
SID SNAME BRANCH PER SNAME ➢ Semicolon (;) : it means end of the query .
1 A ECE 60 A
2 B CSE 75 B
3 C ME 50 C
4 D ECE 80 D
5 C CSE 75 C
6 E CIVIL 95 E
Practice Session
SELECT *
FROM STUDENT ;
➢ WAQTD sname , sid , per , branch of all the students .
EMP
EID ENAME SAL
1 A 100
2 B 200
2 C 100
1. WAQTD name and salary given to the employees .
SELECT ENAME , SAL FROM EMP ;
ENAME SAL*12
A 1200
B 2400
C 1200
4. WAQTD all the details of the employee along with annual salary
Select eid, ename, sal, sal*12
From emp ;
Select
From emp ;
5. WAQTD name and salary with a hike of 20% . Select ename , Sal +
Sal*20/100
From emp ;
"It is an alternate name Which is used to get the user convenient output" .
○ as keyword is used to apply the “alias” and it is optional.
○ Alias can be applied only for the columns in the output, but not in the
table.
○ If the alias contains any special characters, we should used with in double
quotes.
○ Keywords of sql can be used as alias with double quotes.
Example 1 :
Example 2:
Example 3
Example 5:
Example 6
WHERE Clause
❖ Where clause will check the condition for each and every row in the table ( row-by
– row execution )
Example :
➢ SELECT *
FROM EMP
WHERE ENAME ='MILLER' ;
➢ WAQTD details of the employee hired after '01-JAN-1982’
➢ SELECT *
FROM EMP
WHERE HIREDATE > '01-JAN-1982' ;
➢ WAQTD name sal and hiredate of the employees who were
Hired before 1985 .
7.WAQTD NAME AND SAL ALONG WITH HIS ANNUAL SALARY IF THE ANNUAL
SALARY IS MORE THAN 12000
SELECT ENAME , DEPTNO , JOB 7. WAQTD name , job , deptno of the employees working as a manager in dept 10 or
FROM EMP 30 .
WHERE DEPTNO = 10 ;
SELECT ENAME , JOB , DEPTNO FROM EMP
2. WAQTD name and deptno along with job for the WHERE JOB ='MANAGER' AND ( DEPTNO = 10 OR DEPTNO = 20 ) ;
employee working as manager in dept 10 .
8. WAQTD name , deptno , job of the employees working in dept 10 or 20 or 30 as a
SELECT ENAME , DEPTNO , JOB clerk .
FROM EMP
WHERE JOB ='MANAGER' AND DEPTNO = 10 ; SELECT ENAME , JOB , DEPTNO FROM EMP
WHERE JOB ='CLERK' AND ( DEPTNO = 10 OR DEPTNO = 20 AND
DEPTNO = 30 ) ;
ASSIGNMENT ON LOGICAL OPERATORS :
1.WAQTD DETAILS OF THE EMPLOYEES WORKING AS CLERK AND EARNING LESS THAN 1500
2.WAQTD NAME AND HIREDATE OF THE EMPLOYEES WORKING AS MANAGER IN DEPT 30
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.
4.WAQTD ALL THE DETAILS OF THE EMP WORKING IN DEPT 30 OR AS ANALYST
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
7.WAQTD EMPNO AND NAMES OF THE EMPLOYEES WORKING AS MANAGER IN DEPT 20
8.WAQTD DETAILS OF EMPLOYEES WORKING IN DEPT 20 OR 30 .
9.WAQTD DETAILS OF EMPLOYEES WORKING AS ANALYST IN DEPT 10 .
10.WAQTD DETAILS OF EMPLOYEE WORKING AS PRESIDENT WITH SALARY OF RUPEES 4000
SPECIAL OPERATORS :
2. NOT IN : It is a multi-valued operator which can accept
1. IN
2. NOT IN multiple values At the RHS . It is similar to IN op instead of
3. BETWEEN selecting it Rejects the values .
4. NOT BETWEEN
5. IS
6. IS NOT Syntax: Column_Name / Exp NOT IN ( v1 , v2 , . . vn )
7. LIKE
8. NOT LIKE
Example :
1. IN : It is a multi-valued operator which can accept
SELECT ENAME , DEPTNO FROM EMP
multiple values At the RHS . WHERE DEPTNO = 20 AND
JOB NOT IN ( 'CLERK' ,'MANAGER' ) ;
Syntax: Column_Name / Exp IN ( v1 , v2 , . . Vn )
Example :
Syntax: Syntax:
Column_Name BETWEEN Lower_Range AND Higher_Range ;
Column_Name NOT BETWEEN Lower_Range AND Higher_Range ;
Between Op works including the range .
Example :
Example :
➢ WAQTD name and salary of the employees if the emp is not
➢ WAQTD name and salary of the employees if the emp is earning earning Salary in the range 1000 to 3000 .
Salary in the range 1000 to 3000 .
SELECT ENAME , SAL
SELECT ENAME , SAL FROM EMP
FROM EMP WHERE SAL NOT BETWEEN 1000 AND 3000 ;
WHERE SAL BETWEEN 1000 AND 3000 ;
➢ WAQTD name and deptno of the employees working in dept 10
➢ WAQTD name and deptno of the employees working in dept 10 And not hired during 2019 .
And hired during 2019 (the entire year of 2019) .
SELECT ENAME , DEPTNO
SELECT ENAME , DEPTNO FROM EMP
FROM EMP WHERE DEPTNO = 10 AND HIREDATE NOT BETWEEN '01-
WHERE DEPTNO = 10 AND HIREDATE BETWEEN '01- JAN-2019' AND '31-DEC-2019' ;
JAN-2019' AND '31-DEC-2019' ;
➢ WAQTD name , sal and hiredate of the employees who were not
➢ WAQTD name , sal and hiredate of the employees hired during
hired during 2017 into dept 20 with a salary greater that 2000 .
2017 into dept 20 with a salary greater that 2000 .
SELECT ENAME , SAL , HIREDATE
SELECT ENAME , SAL , HIREDATE
FROM EMP
FROM EMP
WHERE DEPTNO = 20 AND SAL> 2000 AND HIREDATE WHERE DEPTNO = 20 AND SAL> 2000 AND HIREDATE NOT
BETWEEN '01-JAN2017' AND 31-DEC-2017' ;
BETWEEN '01-JAN2017' AND 31-DEC-2017' ;
5. IS : "It is used to compare only NULL " 6. IS NOT : "It is used to compare the values with NOT NULL ".
Example : Example :
➢ WAQTD name of the employee who is getting salary .
EID ENAME SAL COMM
1 A 1000 100 SELECT ENAME
2 B null null FROM EMP
3 C null 200 WHERE SAL IS NOT NULL ;
4 D 2000 null ➢ WAQTD name of the emp who gets commission .
➢ WAQTD name of the employee who is not getting salary . SELECT ENAME
FROM EMP
SELECT ENAME WHERE COMM IS NOT NULL ;
FROM EMP
WHERE SAL IS NULL ; ➢ WAQTD name , sal and comm of the emp if the emp doesn’t earn
commission but gets salary .
➢ WAQTD name of the emp who doesn’t get commission .
SELECT ENAME , SAL , COMM
SELECT ENAME FROM EMP
FROM EMP WHERE COMM IS NULL AND SAL IS NOT NULL ;
WHERE COMM IS NULL ;
➢ WAQTD name , sal and comm of the emp if the emp doesn’t earn
both .
To achieve pattern matching we use special characters . ➢ WAQTD names that starts with 'J' and ends with 'S' .
➢ Percentile (%)
➢ Underscore ( _ ) SELECT ENAME
FROM EMP
Syntax: Column_Name LIKE 'pattern' ; WHERE ENAME LIKE 'J%S' ;
Example : ➢ WAQTD names of the employee if the emp has char 'A' as his
second character .
➢ WAQTD details of an employee whose name is SMITH .
SELECT ENAME
SELECT * FROM EMP
FROM EMP WHERE ENAME LIKE '_A%' ;
WHERE ENAME ='SMITH' ;
➢ WAQTD names of the employee if the emp has char 'A' as his Third
➢ WAQTD details of the employee who's name starts with 'S' . character .
➢ WAQTD details of the employee who's name ends with 'S' . ➢ WAQTD names of the employee if the emp has char 'A' as his
second character and 'S' is last character .
SELECT *
FROM EMP SELECT ENAME
WHERE ENAME LIKE '%S' ; FROM EMP
WHERE ENAME LIKE '_A%S' ;
➢ WAQTD names of the employees who have character 'S' in their names
. ➢ WAQTD names of the employee if the emp has char 'A' present at at
least 2 times .
SELECT * FROM
EMP SELECT ENAME
FROM EMP
WHERE ENAME LIKE '%A%A%' ;
➢ WAQTD names of the employee if the emp's salary's last 2 digit is 50 rupees .
SELECT ENAME
FROM EMP
WHERE SAL LIKE '%50' ;