Unit-6 (Manipulating and Querying Data)
Unit-6 (Manipulating and Querying Data)
QUERYING DATA
OUTLINE
❖ Adding Data with INSERT Statement
❖ Retrieving Data with SELECT Statement and FROM Clause and Filter Data with WHERE
Clause
❖ Order and Grouping Data with ORDER and GROUP by Clause and Summarizing the Select
Statement
❖ Retrieving Data from Different Tables using: INNER JOINS, OUTER JOIN and CROSS JOIN
❖ Building Nested Queries
❖ Manipulate Data Using UPDATE Statement and Removing Rows Using DELETE Statement
❖ Creating and Altering View
DATA MANIPULATION LANGUAGE
i. SELECT
ii. INSERT
iii. UPDATE
iv. DELETE
ADDING DATA WITH INSERT STATEMENT
INSERT Statement
❖Syntax: INSERT INTO tablename (column1, column2, ……. ) VALUES
(value1, value2, ……………),
…………………………….. );
SELECT STATEMENT
❖Syntax:
SELECT column1, column2, …… FROM tablename WHERE condition;
UPDATE STATEMENT
❖Syntax:
UPDATE tablename SET column_name = new_value WHERE condition
DELETE STATEMENT
❖Syntax:
DELETE FROM tablename WHERE condition;
DELETE STATEMENT
❖Syntax:
ORDER BY CLAUSE
❖ The order by clause causes the tuples in the result of a query to appear in sorted order.
❖Syntax:
SELECT column1, column2 FROM tablename ORDER BY column ASC/DESC
❖E.g.,
ORDER BY CLAUSE
Using Expression NULL First/ Last
AGGREGATE FUNCTIONS
❖Aggregate functions operate on a multiset of values and return a single value.
❖Typical aggregate functions are min, max, sum, count, and avg.
MIN: Retrieves the minimum value in a column.
Syntax:
SELECT MIN(column_name) FROM table_name;
❖Note: The GROUP BY clause specifies the grouping attributes, which should also appear in
the SELECT clause.
ORDER BY AND GROUP BY
❖Together
SUMMARIZING THE SELECT STATEMENT
Having Clause
❖ The HAVING clause is used in SQL queries to filter the results of aggregate functions applied to
groups defined by the GROUP BY clause.
❖ It is similar to the WHERE clause but is used specifically with grouped data.
Syntax:
SELECT column1,….., aggregate_function(expression),
FROM table_name
GROUP BY column1,…., HAVING condition;
E.g.,: SELECT Country, COUNT(*) FROM Customer GROUP BY Country HAVING COUNT (*) >= 2
SUMMARIZING THE SELECT STATEMENT
Having Clause
SUMMARIZING THE SELECT STATEMENT
SQL AND, OR and NOT Operators
❖ The WHERE clause can be combined with AND, OR and NOT operators.
❖ These operators are used to filter records based on more than one condition.
❖Syntax: SELECT column1,…. FROM tablename WHERE condition1 AND condition2 …..
❖Syntax:
SELECT column1, column2…. FROM tablename WHRERE column LIKE pattern
SUMMARIZING THE SELECT STATEMENT
SQL LIKE Operator
SUMMARIZING THE SELECT STATEMENT
SQL IN Operator
❖ The IN operator allows to specify multiple values in a WHERE clause.
❖ The IN Operator is shorthand for multiple OR conditions
Syntax:
SELECT column1…. FROM tablename WHERE columnname IN (value1, value2, ….)
Syntax:
SELECT column…. FROM tablename WHERE columnname BETWEEN value1 AND value2
RETRIEVING DATA FROM DIFFERENT TABLES
USING: INNER JOINS
❖ In SQL (Structured Query Language), the JOIN operation is used to combine rows from two
or more tables based on a related column between them.
Inner Join
❖ Inner join produces only the set of records that match in both Table A and Table B.
Syntax:
SELECT column1, column2, ...
FROM table1
INNER JOIN table2 ON table1.column = table2.column;
RETRIEVING DATA FROM DIFFERENT TABLES
USING: INNER JOINS
Suppose two table A and table B
RETRIEVING DATA FROM DIFFERENT TABLES
USING: INNER JOINS
Query: SELECT * FROM TableA INNER JOIN TableB ON TableA.PK = TableB.PK
RETRIEVING DATA FROM DIFFERENT TABLES
USING: INNER JOINS
Customers Table Orders Table
Syntax:
SELECT column_name,..,
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name = table2.column_name;
Syntax:
SELECT column_name,…
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name = table2.column_name;
OUTER JOIN
Right Outer Join
❖Syntax: SELECT * FROM TableA RIGHT OUTER JOIN TableB ON TableA.PK = TableB.PK
OUTER JOIN
Full Outer Join
❖ Full outer join produces the set of all records in Table A and Table B, with matching records
from both sides where available.
❖ If there is no match, the missing side will contain null.
Syntax:
SELECT column_name
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;
OUTER JOIN
Full Outer Join
❖Syntax: SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.PK = TableB.PK
CROSS JOIN
❖ A cross join is a Cartesian Product join – it is every record in Table A combined with every
record in Table B.
https://www.scaler.com/topics/nested-sql-query/
NESTED QUERIES
Two different types:
i. Independent Nested Queries
ii. Co-related Nested Queries
❖ NOT IN Operator
SELECT id, name FROM Employees
WHERE id NOT IN (SELECT employee_id FROM Awards);
NESTED QUERIES
❖ ANY and ALL operators: used in SQL to compare a value to a set of values returned by a
subquery.
SELECT * FROM Employees WHERE role = 'Developer'
AND salary > ALL (
SELECT salary FROM Employees WHERE role = 'Manager’
);
❖Consider a clerk who needs to know an instructor's ID, name, and department name, but does not
have authorization to see the instructor's salary amount.
DROP views:
DROP VIEW view_name;
CREATING AND ALTERING VIEW
❖ Suppose a student table
CREATING AND ALTERING VIEW
CREATE VIEW student_view AS SELECT sname ,sid
FROM students;
//Optional where condition [WHERE condition];
❖ Create a view of name student_view that contain only those student name and age of the
student relation of age greater than 24.
E.g.,
CREATE OR REPLACE VIEW student_view AS SELECT sname, age, department_id
FROM student
WHERE age>24;
END OF UNIT 6 Dipesh Koirala