Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

SQL 5

The document discusses several SQL operators and concepts including: 1) The IN operator which allows specifying multiple values in a WHERE clause to filter records. Examples demonstrate retrieving records by department number or employees with certain salaries. 2) Set operations like UNION, INTERSECT, and EXCEPT which are used to combine result sets based on matches or differences in values. 3) How NULL values are handled in arithmetic, logical expressions, and SELECT statements using operators like IS NULL and IS NOT NULL. 4) The BETWEEN operator which selects values within a specified numeric, text, or date range. Examples show filtering records by salary amount or hiring date.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

SQL 5

The document discusses several SQL operators and concepts including: 1) The IN operator which allows specifying multiple values in a WHERE clause to filter records. Examples demonstrate retrieving records by department number or employees with certain salaries. 2) Set operations like UNION, INTERSECT, and EXCEPT which are used to combine result sets based on matches or differences in values. 3) How NULL values are handled in arithmetic, logical expressions, and SELECT statements using operators like IS NULL and IS NOT NULL. 4) The BETWEEN operator which selects values within a specified numeric, text, or date range. Examples show filtering records by salary amount or hiring date.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

SCHEMA for every example in the latter slides

• Employee(FNAME, LNAME, SSN, Bdate, Address, Sex, Salary, Superssn, Dno)


• Department(Dname, Dnumber, Mgrssn, MgrStartDate)
• Dept_Locations(Dnumber, Dlocation)
• Project(Pname, Pnumber, Plocation, Dnum)
• WORKS_ON(essn, Pno, Hours)
• Dependent(essn, Dependent_name, sex, bdate, Relationship)
IN Operator
• The IN Operator allows you to specify multiple values in a WHERE clause.
• Syntax: SELECT column_name(s) FROM Table_name
WHERE Column_name IN(val1, val2,….);
• Ex: Retrieve the Fnames of employees who works for the departments 2,3,4,5.
➢ SELECT Fnames FROM Employee WHERE Dno IN(2,3,4,5);
• Ex: Find Fname, Address of the employees who work for the department located in ‘Newyork’
➢ SELECT Fname, Address FROM Employee
WHERE Dno IN( SELECT Dnumber FROM Dept_Locations
WHERE Dlocation = ‘Newyork’);
• Ex: Delete the records of employees who works for the departments 6,7,8.
➢ DELETE FROM Employee WHERE Dno IN(6,7,8);
• Ex: Find second highest salary from Employee Table.
➢ SELECT MAX(Salary) FROM Employee
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee);
Set Operations
• UNION
• INTERSECT
• EXCEPT

• UNION ALL
• INTERSECT ALL
• EXCEPT ALL
Set Operations

studId courseNAME Grade


1 Maths A
2 Physics A
3 Chemistry C
2 Maths B
2 Biology B
1 Chemistry A
3 Physics D
2 Chemistry A
2 History A
Set Operations
• Retrieve the list of studID’s who get either grade ‘A’ or grade ‘B’

• SELECT studID FROM Enroll WHERE Grade = ‘A’


UNION
SELECT studID FROM Enroll WHERE Grade = ‘B’;
Set Operations
• Retrieve the list of studID’s who get both grade ‘A’ and grade ‘B’

• SELECT studID FROM Enroll WHERE Grade = ‘A’


INTERSECT
SELECT studID FROM Enroll WHERE Grade = ‘B’;
Set Operations (EXCEPT or MINUS)
• Retrieve the list of studID’s who get grade ‘A’ but not grade ‘B’

• SELECT studID FROM Enroll WHERE Grade = ‘A’


EXCEPT
SELECT studID FROM Enroll WHERE Grade = ‘B’;
Dealing with NULL values in various contexts
• Arithmetic expressions (+, - , *, /)

A A
5 6
4 5
NULL NULL
1 2
Dealing with NULL values in various contexts
• Logical expressions

A B A AND B A OR B
T T T T
T F F T
F T F T
F F F F
T UK UK T
F UK F UK
UK UK UK UK
Dealing with NULL values in various contexts
• SELECT A FROM R WHERE B = NULL;
A B
a 1
b 2
• SELECT A FROM R WHERE B IS NULL;
c NULL
d NULL

• SELECT A FROM R WHERE B IS NOT NULL;


Dealing with NULL values in various contexts
• SELECT B FROM R;
A B
a 1
• SELECT DISTINCT B FROM R; b 2
c NULL
d NULL


BETWEEN
• It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.
• WHERE Column_name BETWEEN Val_1 AND Val_2;
• It is a comparison operator on numeric domain.
❖ Retrieve all the employees whore salary is between 30,000 and 50,000
➢ SELECT * FROM Emp WHERE salary BETWEEN 30000 AND 50000;
❖ Insert records into Sample table from Emp table whose salary is between
20000 and 30000 given that both tables have same structure.
➢ INSERT INTO SSAMPLE(SELECT * FROM Emp
WHERE salary BETWEEN 20000 AND 30000);
❖ Retrieve all the employees whore salary is not between 30,000 and 50,000
➢ SELECT * FROM Emp WHERE salary NOT BETWEEN 30000 AND 50000;
BETWEEN
• It is also a comparison operator on text and dates

❖ SELECT * FROM Emp WHERE emp_name BETWEEN ‘A’ AND ‘D’;


BETWEEN
❖ SELECT * FROM Emp WHERE hire-date BETWEEN
‘01-JAN-2022’ AND ‘31-DEC-2022’;

You might also like