You are on page 1of 33

SQL - 2

Er. Pradip Kharbuja

What is NULL?
NULL means Unknown or nonexistent.
NULL is not a value.
It is not zero or blank or an empty string.
NULL is a special marker used in SQL to indicate that a data
value does not exist in the database.
If A and B are NULL, what is the result of A = B?

Data Definition
CREATE TABLE departments (
dept_no INTEGER NOT NULL,
department_name VARCHAR(30),

location VARCHAR(30)
PRIMARY KEY (dept_no)
);

Primary Key
Unique Value + Not NULL = Primary Key
A primary key cannot allow NULL.
Each table can have at most one primary key.

Modifying Tables Using SQL


Add an extra column
Drop a column from a table
Modify the maximum length of the column

Add a new constraint like Primary Key, Unique Key


Drop a constraint
Set a default value for a column
Drop a default value for a column

Adding & Removing new column


ALTER TABLE departments ADD department_head
VARCHAR(30);

ALTER TABLE departments ALTER COLUMN


department_head VARCHAR(50);
ALTER TABLE departments DROP COLUMN
department_head;

Data Retrieving
Select
Order By
Aggregate functions

Group by
Subqueries
Joins

SELECT
SELECT * FROM departments;
SELECT * FROM departments WHERE dept_no = 1;
SELECT dept_no, department_name FROM departments;
SELECT department_name FROM departments WHERE
dept_no = 2;

ORDER BY
to get result in ascending or descending order.
SELECT * FROM departments ORDER BY dept_no;
SELECT * FROM departments ORDER BY dept_no ASC;

SELECT * FROM departments ORDER BY dept_no DESC;


SELECT department_name, location FROM departments WHERE
dept_no = 1 ORDER BY department_name DESC;

workers Table
Column Name
emp_no
first_name
last_name
job_title

Type

age
dept_no

Integer
Integer

Integer
Varchar
Varchar
varchar

Length
30
30
30

NULL

Key

Not Null

Primary Key

workers Table Creation Query

workers Table
emp_no first_name last_name job_title

age

dept_no

1
2
3
4

Kiran
Sandeep
Srijana
Aashish

Mishra
Khanal
Shrestha
Magar

Manager
Manager
Manager
Packer

56
33
32
23

1
2
3
1

5
6
7

Krishna
Hari
Sarala

Dhakal
Shakya
Shrestha

Packer
Accountant
Admin Assistant

24
56
34

1
2
3

Amul

Dahal

Packer

26

NULL

Insert Queries

SQL Comparison Operators


1. =

3. >

5. >=

2. <

4. <=

6. <> or !=

SELECT * FROM workers;


SELECT * FROM workers WHERE age = 56;
SELECT * FROM workers WHERE age > 23;
SELECT * FROM workers WHERE age >= 23;
SELECT * FROM workers WHERE age < 56;
SELECT * FROM workers WHERE age <= 56;
SELECT * FROM workers WHERE age <> 56;
SELECT * FROM workers WHERE age != 56;

SQL Logical Operators


1. AND
2. OR

3. IN
4. NOT

5. BETWEEN
6. IS NULL

SELECT * FROM workers WHERE age = 56 AND dept_no = 1;


SELECT * FROM workers WHERE age = 56 OR dept_no = 1;
SELECT * FROM workers WHERE first_name = 'Hari' OR first_name =
'Sarala' OR first_name = 'Kiran' OR first_name = 'Sandeep';
SELECT * FROM workers WHERE first_name IN ('Hari', 'Sarala', 'Kiran',
'Sandeep');
SELECT * FROM workers WHERE first_name NOT IN ('Hari', 'Sarala',
'Kiran', 'Sandeep');

SQL Logical Operators


1. AND

3. IN

5. BETWEEN

2. OR

4. NOT

6. IS NULL

SELECT * FROM workers WHERE age >= 32 AND age <= 56;
SELECT * FROM workers WHERE age BETWEEN 32 AND 56;
SELECT * FROM workers WHERE dept_no IS NULL;
SELECT * FROM workers WHERE dept_no IS NOT NULL;

LIKE Operator
SELECT * FROM workers WHERE job_title = 'Manager';
SELECT * FROM workers WHERE job_title LIKE 'Manager';
SELECT * FROM workers WHERE first_name LIKE 'S%';

SELECT * FROM workers WHERE first_name LIKE '%a';


SELECT * FROM workers WHERE first_name LIKE 'S%a';
SELECT * FROM workers WHERE first_name LIKE '%ar%';

SELECT * FROM workers WHERE first_name LIKE '_ar%';


SELECT * FROM workers WHERE first_name LIKE '_ar_';

Aggregation Functions
1.

Count returns number of values in a column

2.

Sum returns the sum total of values of a column

3.

Avg returns the mean average of values in column

4.

Min returns the lowest value in a column

5.

Max returns the highest value in a column

Aggregation Functions
SELECT COUNT(emp_no) FROM workers;
SELECT COUNT(emp_no) AS number_of_workers FROM
workers;
SELECT SUM(age) FROM workers;
SELECT AVG(age) FROM workers;
SELECT MAX(age) FROM workers;
SELECT MIN(age) FROM workers;

GROUP BY
Display dept_no and count the number of workers in that
department.

SELECT dept_no, COUNT(emp_no) FROM workers;


This query will produce an error.

SELECT dept_no, COUNT(emp_no) FROM workers GROUP


BY dept_no;

GROUP BY
SELECT dept_no, COUNT(dept_no) FROM workers WHERE
dept_no >=2 GROUP BY dept_no;

SELECT dept_no, COUNT(dept_no) FROM workers WHERE


dept_no >=2 GROUP BY dept_no ORDER BY dept_no DESC;
Display dept_no which has more than 2 workers.
SELECT dept_no FROM workers GROUP BY dept_no HAVING
COUNT(emp_no) > 2;

Sub Queries
A Subquery or Inner query or Nested query is a query within
another SQL query and embedded within the WHERE
clause.
Subqueries must be enclosed within parentheses.
A subquery can have only one column in the SELECT clause

Subqueries that return more than one row can only be used
with multiple value operators, such as the IN operator

Sub Queries
Display the department_name of the depeartment having
the highest dept_no.

SELECT department_name FROM departments WHERE


dept_no = (SELECT MAX(dept_no) FROM departments);

Joins
An SQL JOIN clause is used to combine rows from two or
more tables, based on condition.

Types of Join
1.
2.
3.
4.
5.

Cross Join or Cartesian Product


Inner Join
Left Join
Right Join
Full Join

Cartesian Join or Cross Join


Concept is like Cartesian Product.
eg. A = {1, 2, 3}, B = {a, b}
Cartesian Product will be :
A x B = {(1, a), (1, b), (2, a), (2, b), (3, a), (3, b)}

Cartesian Join or Cross Join


SELECT * FROM departments, workers;

Inner Join
SELECT * FROM departments, workers WHERE
departments.dept_no = workers.dept_no;

Using Alias
SELECT * FROM departments d, workers w WHERE d.dept_no =
w.dept_no;
Select the department_name from departments and the first_name,
last_name and job_title from workers.
SELECT d.department_name, w.first_name, w.last_name, w.job_title
FROM departments d, workers w WHERE d.dept_no = w.dept_no;

Database Questions
1. Select the department_name and location from departments and
the first_name and last_name from workers.
2. Select the department_name and location from departments and
the first_name and last_name from workers. Only select workers
who are in the Packing department.
3. Select the department_name and location from departments, and
the first_name, last_name and job_title from workers, for just the
managers that work in Cairo.
4. Select the job_title, age and location for all the workers who work in
London.

5. Using the COUNT function and joining the two tables, count how
many workers there are in Lagos.

Key clauses in an SQL SELECT statement


SELECT specifies which columns from the table are to appear
in the result
FROM specifies which table or tables are to be used to get the
results

WHERE specifies some condition that will restrict the rows that
are retrieved
GROUP BY groups rows by some column value

HAVING used to restrict the result that will be grouped


ORDER BY specifies the order in which the result will appear

Query Optimisation
Making sure a query runs as efficiently and as quickly as
possible

The query optimizer attempts to determine the most


efficient way to execute a given query by considering the
possible query plans.
eg. TOAD
Available at http://www.quest.com/toad/

ANY QUESTIONS?

References
http://www.tutorialspoint.com/sql/sql-operators.htm

You might also like