topic 08 : sql - 2

33
SQL - 2 Er. Pradip Kharbuja

Upload: pradip-kharbuja

Post on 26-Jan-2015

115 views

Category:

Education


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Topic 08 : SQL - 2

SQL - 2

Er. Pradip Kharbuja

Page 2: Topic 08 : SQL - 2

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?

Page 3: Topic 08 : SQL - 2

Data Definition

CREATE TABLE departments (

dept_no INTEGER NOT NULL,

department_name VARCHAR(30),

location VARCHAR(30)

PRIMARY KEY (dept_no)

);

Page 4: Topic 08 : SQL - 2

Primary Key

Unique Value + Not NULL = Primary Key

A primary key cannot allow NULL.

Each table can have at most one primary key.

Page 5: Topic 08 : SQL - 2

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

Page 6: Topic 08 : SQL - 2

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;

Page 7: Topic 08 : SQL - 2

Data Retrieving

Select

Order By

Aggregate functions

Group by

Subqueries

Joins

Page 8: Topic 08 : SQL - 2

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;

Page 9: Topic 08 : SQL - 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;

Page 10: Topic 08 : SQL - 2

workers Table

Column Name Type Length NULL Key

emp_no Integer Not Null Primary Key

first_name Varchar 30

last_name Varchar 30

job_title varchar 30

age Integer

dept_no Integer

Page 11: Topic 08 : SQL - 2

workers Table Creation Query

Page 12: Topic 08 : SQL - 2

workers Table

emp_no first_name last_name job_title age dept_no

1 Kiran Mishra Manager 56 1

2 Sandeep Khanal Manager 33 2

3 Srijana Shrestha Manager 32 3

4 Aashish Magar Packer 23 1

5 Krishna Dhakal Packer 24 1

6 Hari Shakya Accountant 56 2

7 Sarala Shrestha Admin Assistant 34 3

8 Amul Dahal Packer 26 NULL

Page 13: Topic 08 : SQL - 2

Insert Queries

Page 14: Topic 08 : SQL - 2

SQL Comparison Operators

1. =

2. <

3. >

4. <=

5. >=

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;

Page 15: Topic 08 : SQL - 2

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');

Page 16: Topic 08 : SQL - 2

SQL Logical Operators

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;

1. AND

2. OR

3. IN

4. NOT

5. BETWEEN

6. IS NULL

Page 17: Topic 08 : SQL - 2

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_';

Page 18: Topic 08 : SQL - 2

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

Page 19: Topic 08 : SQL - 2

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;

Page 20: Topic 08 : SQL - 2

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;

Page 21: Topic 08 : SQL - 2

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;

Page 22: Topic 08 : SQL - 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

Page 23: Topic 08 : SQL - 2

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);

Page 24: Topic 08 : SQL - 2

Joins

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

Types of Join1. Cross Join or Cartesian Product

2. Inner Join

3. Left Join

4. Right Join

5. Full Join

Page 25: Topic 08 : SQL - 2

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)}

Page 26: Topic 08 : SQL - 2

Cartesian Join or Cross Join

SELECT * FROM departments, workers;

Page 27: Topic 08 : SQL - 2

Inner Join

SELECT * FROM departments, workers WHERE

departments.dept_no = workers.dept_no;

Page 28: Topic 08 : SQL - 2

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_titleFROM departments d, workers w WHERE d.dept_no = w.dept_no;

Page 29: Topic 08 : SQL - 2

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.

Page 30: Topic 08 : SQL - 2

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

Page 31: Topic 08 : SQL - 2

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/

Page 32: Topic 08 : SQL - 2

ANY QUESTIONS?

Page 33: Topic 08 : SQL - 2

References

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