DBMS Module 3
DBMS Module 3
SQL
Introduction to SQL
2
What is SQL
• SQL stands for Structured Query Language.
• It is designed for managing data in a relational database
management system (RDBMS).
• It is pronounced as S-Q-L or sometime See-Qwell.
• SQL is a database language, it is used for database creation,
deletion, fetching rows, and modifying rows, etc.
• SQL is based on relational algebra and tuple relational
calculus.
• All RDBMS like MySQL, Oracle, MS Access, Sybase, Informix,
Postgres, and SQL Server use SQL as standard database
language.
3
Why SQL is required
4
Tables and Views
5
SQL Statements
•
• SELECT
• INSERT
• UPDATE Data manipulation language (DML)
• DELETE
• CREATE
• ALTER
• DROP Data definition language (DDL)
• RENAME
• TRUNCATE
• Columns can be also be given new name with the use of ALTER TABLE.
Syntax(MySQL, Oracle)
DEPARTMENTS LOCATIONS
department_id location_id
department_name street_address
manager_id postal_code city
location_id state_province
country_id
JOB_HISTORY
employee_id
start_date EMPLOYEES
end_date employee_id
job_id first_name COUNTRIES
department_id last_name email country_id
phone_number country_name
hire_date region_id
job_id
salary
commission_pct
JOBS manager_id
job_id department_id
job_title REGIONS
min_salary region_id
max_salary region_name
DML Commands
• INSERT INTO TABLE_NAME [(column1, column2, column3,...columnN)] VALUES (value1, value2,
value3,...valueN);
OR
• INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);
• Insert Multiple Rows
INSERT ALL
INTO table_name (column1, column2, column_n) VALUES (expr1, expr2, expr_n)
19
Oracle CREATE VIEW
Syntax:
CREATE VIEW view_name AS SELECT columns FROM tables
WHERE conditions;
Example:
CREATE TABLE "SUPPLIERS"
( "SUPPLIER_ID" NUMBER,
"SUPPLIER_NAME" VARCHAR2(4000),
"SUPPLIER_ADDRESS" VARCHAR2(4000)
);
20
Cont..
CREATE TABLE "ORDERS"
( "ORDER_NO." NUMBER,
"QUANTITY" NUMBER,
"PRICE" NUMBER
);
Input the records…………….Then…………….
Create View Query:
CREATE VIEW sup_orders AS
SELECT suppliers.supplier_id, orders.quantity, orders.price
FROM suppliers
INNER JOIN orders
ON suppliers.supplier_id = supplier_id
WHERE suppliers.supplier_name = 'VOJO';
21
Cont..
You can now check the Oracle VIEW by this query:
SELECT * FROM sup_orders;
22
Oracle Update VIEW
In Oracle, the CREATE OR REPLACE VIEW statement is used to
modify the definition of an Oracle VIEW without dropping it.
Syntax:
23
Cont..
Example:
Execute the following query to update the definition of Oracle VIEW called
sup_orders without dropping it.
Example:
25
Joins
• The basic syntax of INNER JOIN is as follows:
• SELECT table1.column1, table2.column2... FROM table1 INNER
JOIN table2 ON table1.common_filed = table2.common_field;
• SQL> SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS
INNER JOIN ORDERS ON CUSTOMERS.ID =
ORDERS.CUSTOMER_ID;
30
Oracle ORDER BY Example: (sorting in descending
order)
31
SQL Aggregate Functions
• COUNT Function
SELECT COUNT(*) FROM Customers;
SELECT COUNT(*) FROM Customers WHERE salary>=2000;
• SUM Function
SELECT SUM(Salary) FROM Customers;
SELECT SUM(Salary) FROM Customers
WHERE salary>=2000;
• AVG function
SELECT AVG(Salary) FROM Customers;
• MAX Function
SELECT MAX(Salary) FROM Customers;
• MIN Function
SELECT MIN(Salary) FROM Customers;
Oracle GROUP BY Clause
In Oracle GROUP BY clause is used with SELECT statement
to collect data from multiple records and group the
results by one or more columns.
Syntax:-
SELECT column1, column2 FROM table_name WHERE [ conditions ]
GROUP BY column1, column2
SQL> SELECT NAME, SUM(SALARY) FROM CUSTOMERS GROUP BY
NAME;
33
Oracle GROUP BY Example: (with COUNT function)
Customer
O/P
SELECT department,
MIN(salary) AS "Lowest salary"
FROM employees
GROUP BY department;
35
Oracle GROUP BY Example: (with MAX function)
EMPLOYEES
SELECT department,
MAX(salary) AS "Highest salary"
FROM employees
GROUP BY department;
36
Oracle HAVING Clause
In Oracle, HAVING Clause is used with GROUP BY Clause to
restrict the groups of returned rows where condition is TRUE.
SELECT expression1, expression2, ... expression_n,
aggregate_function (aggregate_expression)
FROM tables
WHERE conditions
GROUP BY expression1, expression2, ... expression_n
HAVING having_condition;
*********************************
aggregate_function : SUM, COUNT, MIN, MAX or AVG functions.
37
Oracle HAVING Example: (with GROUP BY COUNT function)
Customer O/P
EMPLOYEES
SELECT department,
MIN(salary) AS "Lowest salary"
FROM employees
GROUP BY department
HAVING MIN(salary) < 15000; 39
Oracle HAVING Example: (with GROUP BY MAX function)
EMPLOYEES
40
LIKE Clause
• SQL LIKE clause is used to compare a value to similar
values using wildcard operators.
• There are two wildcards used in conjunction with the
LIKE operator:
• The percent sign (%) - The percent sign represents zero,
one, or multiple characters.
• The underscore (_) - The underscore represents a single
number or character. The symbols can be used in
combinations.
TOP Clause
• EXCEPT Clause
• SELECT column1 [, column2 ] FROM table1 [, table2 ] [WHERE
condition] EXCEPT SELECT column1 [, column2 ] FROM table1 [,
table2 ] [WHERE condition]
SQL> SELECT City FROM Customers
EXCEPT
SELECT City FROM Suppliers
ORDER BY City;
Alias
• Queries:
1. find those employees who get higher salary than the employee
whose ID is 163.
2. find those employees whose salary matches the smallest salary
of any of the departments.
3. find those employees who report that manager whose first
name is ‘Ramesh’.
4. find the employee whose salary is 3000 and reporting person’s
ID is 121.
1. find those employees whose ID matches any of the
number 134, 159 and 183.
2. find those employees who do not work in those
departments where manager ids are in the range 100,
200.
3. find those employees who get second-highest salary.
4. find those employees who work in the same department
where ‘Clara’ works.
5. find those employees who work in a department where
the employee’s first name contains a letter 'T‘.
6. find those employees who earn more than the average
salary and work in a department with any employee
whose first name contains a character a 'J'.
Answer
1. SELECT first_name, last_name FROM employees WHERE salary >
( SELECT salary FROM employees WHERE employee_id=163 );
2. SELECT first_name, last_name, salary, department_id FROM
employees WHERE salary IN ( SELECT MIN(salary) FROM
employees GROUP BY department_id );
3. SELECT first_name, last_name, employee_id, salary FROM
employees WHERE manager_id = (SELECT employee_id FROM
employees WHERE first_name = ‘Ramesh' );
4. SELECT * FROM employees WHERE (salary,manager_id)= (SELECT
3000,121);
5. SELECT * FROM employees WHERE employee_id IN (134,159,183);
6. SELECT * FROM employees WHERE department_id NOT IN (SELECT
department_id FROM departments WHERE manager_id BETWEEN
100 AND 200);
Answer
1. SELECT * FROM employees WHERE employee_id IN (SELECT
employee_id FROM employees WHERE salary = (SELECT
MAX(salary) FROM employees WHERE salary < (SELECT
MAX(salary) FROM employees)));
2. SELECT first_name, last_name, hire_date FROM employees
WHERE department_id = ( SELECT department_id FROM
employees WHERE first_name = 'Clara') AND first_name <>
'Clara';
3. SELECT employee_id, first_name, last_name FROM employees
WHERE department_id IN ( SELECT department_id FROM
employees WHERE first_name LIKE '%T%' );
4. SELECT employee_id, first_name , salary FROM employees
WHERE salary > (SELECT AVG (salary) FROM employees ) AND
department_id IN ( SELECT department_id FROM employees
WHERE first_name LIKE '%J%');