Professional Documents
Culture Documents
Management System
(integrity constraint1)
………….,
(integrity constraintk));
Integrity constraint in create table
Not null create table department(
dept_name varchar (20),
Primary key
building varchar (15),
Foreign key budget numeric (12,2),
Example: primary key (dept name));
• DML Command
Total: sum(): return the total (sum) of the given row under given
condition
Example :
Select sum(salary) from teacher where
dept_name=‘IT Dept’;
Cont…
Count: count() : count the total no. of tuple in specified rows.
Example :Select count(teacher_id) from teacher where dept_name=‘IT
Dept’;
Note: You can use multiple condition in aggregate function and use ‘distinct’
keyword to remove duplicate values in count ().
** Aggregation with null values: count () ignore null values
Distinct keyword
5000
8000
10000
Example to Perform Simple Calculations
using Select Query
• Consider the following Employee table eid Name age salary
• CREATE or REPLACE view sale_view as select * from Sale where customer = 'Alex';
SQL View
• Example of Displaying a View
• Syntax of displaying a view is similar to fetching data from table using
Select statement.
• SELECT * from sale_view;
• Force View Creation
• force keyword is used while creating a view. This keyword force to create View
even if the table does not exist. After creating a force View if we create the
base table and enter values in it, the view will be automatically updated.
• Syntax for forced View is,
• CREATE or REPLACE force view view_name AS SELECT column_name(s) FROM
table_name WHERE condition
SQL View
• Update a View
• Update command for view is same as for tables.
• Syntax to Update a View is,
• UPDATE view-name set value WHERE condition;
• If we update a view it also updates base table data automatically.
• Read-Only View
• We can create a view with read-only option to restrict access to the view.
• Syntax to create a view with Read-Only Access
• CREATE or REPLACE force view view_name AS SELECT column_name(s) FROM
table_name WHERE condition with read-only
• The above syntax will create view for read-only purpose, we cannot Update or
Insert data into read-only view. It will throw an error.
SQL View
• There are two types of view,
• Simple View
• Complex View
Simple View Complex View
• Here we want to find name and age of employees grouped by their salaries
• SQL query for the above requirement will be,
SELECT name, age Name age
Shane 29
Anu 22
Example of Group by in a Statement with
WHERE clause
• Consider the following Emp table
eid name Age salary
Scott 9000
Group By clause
Example:
Select count(student_id), college_name from
college group by college_name;
• Suppose we want to find the customer whose previous_balance sum is more than 1000.
• We will use the below SQL query,
• SELECT * from sale group customer having sum(previous_balance) > 1000
oid order_name previous_balance customer
• On Delete Cascade :
• This will remove the record from child table, if that value of foreign key is deleted from the main table.
• On Delete Null :
• This will set all the values in that record of child table as NULL, for which the value of foreign key is
seleted from the main table.
• If we don't use any of the above, then we cannot delete data from the main table for which
data in child table exists. We will get an error if we try to do so.
ERROR : Record in child table exist
SQL CHECK Constraint
• The CHECK constraint is used to limit the value range that can be placed in a column.
• If we define a CHECK constraint on a single column it allows only certain values for this column.
• If we define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns
in the row.
• SQL CHECK on CREATE TABLE
• Example
• MySQL:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
• SQL Server / Oracle / MS Access:
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int CHECK (Age>=18)
);
SQL CHECK Constraint
• Defining a CHECK constraint on multiple columns
• Example
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255),
CONSTRAINT CHK_Person CHECK (Age>=18 AND City like'Kath%’)
);
We can use nested subqueries in where clause as well as from clause.
A subquery can have only one column in the SELECT clause, unless multiple
columns are in the main query for the subquery to compare its selected
columns.
Subqueries that return more than one row can only be used with multiple
value operators such as the IN operator.
Cont…
The SELECT list cannot include any references to values that evaluate
to a ARRAY.
Result
ID NAME ID Address
1 abhi 1 DELHI
2 adam 1 DELHI
SELECT *
from class, 4 alex 1 DELHI
cross JOIN class_info; 1 abhi 2 MUMBAI
Or 2 adam 2 MUMBAI
Select * from class, class_info; 4 alex 2 MUMBAI
1 abhi 3 CHENNAI
2 adam 3 CHENNAI
4 alex 3 CHENNAI
INNER Join or EQUI Join
Returns rows when there is a match in both tables.
This is a simple JOIN in which the result is based on matched data as per the equality condition
specified in the query.
Inner Join Syntax
SELECT column-name-list from table-name1
INNER JOIN
table-name2 WHERE table-name1.column-name = table-name2.column-name;
OR
Select table1.column1, table2.column3 from table1, table2
where table1.columnn = table2.columnn
Inner join example:
SELECT * from class, class_info where class.id = class_info.id;
OR
SELECT name, products.product_name FROM person INNER JOIN purchase
ON person.id = purchase.id inner join products on
purchase.product_id=products.product_id;
Example
Class table, Class_info table,
ID NAME ID Address
1 Abhi 1 DELHI
2 Adam 2 MUMBAI
3 Alex 3 CHENNAI
4 Anu
Result
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
Left Join
LEFT JOIN − returns all rows from the left table, even if there are no matches in the right
table. The join will still return a row in the result, but with NULL in each column from the
right table.
The left outer join returns a result table with the match data of two tables then remaining
rows of the left table and the null value for right table's column.
Left join Syntax:
SELECT column_list from table1 LEFT OUTER JOIN /LEFT JOIN table2 ON
table1.column_m = table2.column_p
Left join example:
SELECT name, products.product_name
from person LEFT JOIN purchase
on person.id=purchase.id
LEFT JOIN products on purchase.product_id=products.product_id;
OR
SELECT * FROM student LEFT JOIN address ON student.id = address.id;
Right Join
Returns all rows from the right table, even if there are no matches in the right table. The join
will still return a row in the result, but with NULL in each column from the left table.
The right outer join returns a result table with the match data of two tables then remaining
rows of the right table and the null value for left table's column.
Syntax:
SELECT column_list from table1 RIGHT OUTER JOIN /RIGHT JOIN table2 ON table1.column_m =
table2.column_p
Right join example:
SELECT name, products.product_name
from person RIGHT JOIN purchase
on person.id=purchase.id
RIGHT JOIN products on purchase.product_id=products.product_id;
OR
SELECT * FROM student RIGHT JOIN address ON student.id =
address.id;
Full Join/ FULL OUTER JOIN
Return row when there is a match in one of the tables.
The FULL OUTER JOIN returns a result table with the match data of two tables
then remaining rows of both left table and then the right table.
Syntax:
SELECT column_list from table1 FULL OUTER JOIN /FULL JOIN
table2 ON table1.column_m = table2.column_p
Full join example:
SELECT name, products.product_name
from person FULL JOIN purchase
on person.id=purchase.id
FULL JOIN products on purchase.product_id=products.product_id;
OR
SELECT * FROM student FULL JOIN address ON student.id =
address.id;
SELF JOIN
• A self join is a join in which a table is join with itself. Self join is used
for comparison within a table.
• Syntax:
• SELECT column_list FROM table_name A, table_name B WHERE condition.
• Example:
• SELECT a.fname AS FirstName1, b.fname AS FirstName2 FROM student as a,
student as b WHERE a.fname<>b.fname AND a.address=b.address
• SELECT a.fname AS FirstName1, b.fname AS FirstName2 FROM student as a,
student as b WHERE a.fname!=b.fname AND a.address=b.address