Er. Pradip Kharbuja
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.
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;
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
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
3. >
5. >=
2. <
4. <=
6. <> or !=
3. IN
4. NOT
5. BETWEEN
6. IS NULL
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%';
Aggregation Functions
1.
2.
3.
4.
5.
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.
GROUP BY
SELECT dept_no, COUNT(dept_no) FROM workers WHERE
dept_no >=2 GROUP BY dept_no;
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.
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.
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.
WHERE specifies some condition that will restrict the rows that
are retrieved
GROUP BY groups rows by some column value
Query Optimisation
Making sure a query runs as efficiently and as quickly as
possible
ANY QUESTIONS?
References
http://www.tutorialspoint.com/sql/sql-operators.htm