0% found this document useful (0 votes)
4 views

SQL-Advanced Queries

The document provides an overview of advanced SQL queries, including table creation, constraints (NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY), and operations like ALTER TABLE and DROP TABLE. It also covers the use of VIEWS, UNION, INTERSECT, MINUS, and various types of JOINs. Additionally, it includes examples and syntax for creating and manipulating tables and constraints in SQL.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL-Advanced Queries

The document provides an overview of advanced SQL queries, including table creation, constraints (NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY), and operations like ALTER TABLE and DROP TABLE. It also covers the use of VIEWS, UNION, INTERSECT, MINUS, and various types of JOINs. Additionally, it includes examples and syntax for creating and manipulating tables and constraints in SQL.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

SQL Advanced Queries

CONTENTS
CREATE TABLE query
NOT NULL constraint
UNIQUE constraint
PRIMARY KEY constraint
FOREIGN KEY constraint
Deleting constraints
ALTER TABLE query
DROP TABLE
VIEWS – creating/updating/dropping
UNION and UNION ALL
INTERSECT
MINUS
NATURAL JOIN
JOIN/INNER JOIN
LEFT OUTER JOIN & RIGHT OUTER JOIN
• APPENDIX A,B,C
CREATE TABLE
CREATE TABLE table_name
(column_name1 data_type,
column_name2 data_type,..)

Write a query to create table students with the fields
Student_id,First_name,Last_name,marks obtained,Grade,Class,date of
birth and thus know about the data types.
CREATE TABLE From Another Table
Copying all columns from another table:
CREATE TABLE new_table_name
AS (SELECT * FROM old_table);
Copying selected columns from another table:
CREATE TABLE new_table_name
AS (SELECT * FROM old_table where condition);
Write a query to create table stud with the fields Student_id, First_name,
Last_name,date of birth of students table where class > 5
write another query to create a table stud1 with all the fields of students table
Copying selected columns from multiple tables:
CREATE TABLE dept AS
(SELECT e.Emp_ID,e.First_name,e.Last_name,d.Dept_ID,d.Salary
FROM employees e,departments d WHERE e.Emp_ID = d.Emp_ID);
CONSTRAINTS

Constraints are used to limit the type of data that can go into a table.

Constraints can be specified when a table is created, with the CREATE
TABLE statement or after the table is created ,with the ALTER TABLE
statement. Constraints can also be defined in column level or table-level.
Note: For Alter table, only future rows will be affected
TYPES:
1. NOT NULL
2. UNIQUE
3. PRIMARY KEY
4. FOREIGN KEY
5. CHECK
6. DEFAULT
NOT NULL CONSTRAINT

The NOT NULL constraint enforces a column to NOT accept NULL values.

The NOT NULL constraint enforces a field to always contain a value. You
cannot insert a new record, or update a record without adding a value to this
field.

The following SQL enforces the "P_Id" column and the “First_Name" column
to not accept NULL values: (column level)
CREATE TABLE Persons
(P_Id number(10) NOT NULL,
LastName varchar(255) ,
FirstName varchar(255) NOT NULL,
Address varchar(255),
City varchar(255) )
UNIQUE CONSTRAINT

The UNIQUE constraint uniquely identifies each record in a database table.

It can be defined by table level and column level

It can contain NULL values
Column Level:
CREATE TABLE Persons
(P_Id number(10) UNIQUE,
LastName varchar(255) ,
FirstName varchar(255) )
Table-level:
CREATE TABLE Persons
(P_Id number(10),
LastName varchar(255) ,
FirstName varchar(255)
CONSTRAINT unique_P_ID UNIQUE(P_Id) )
UNIQUE CONSTRAINT

Specifying Constraint in “ALTER TABLE” Statement:


ALTER TABLE Persons
ADD UNIQUE (P_Id)
To specify constraint name:
ALTER TABLE Persons
ADD CONSTRAINT uc_PersonID UNIQUE (P_Id)
Constraint to more than one field:
ALTER TABLE Persons
ADD CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)
PRIMARY KEY & FOREIGN KEY
CONSTRAINT
PRIMARY KEY:

Primary keys must contain unique values.

A primary key column cannot contain NULL values.

Each table may or may not have a primary key, But each table can have
only ONE primary key
FOREIGN KEY:

A FOREIGN KEY in one table points to a PRIMARY KEY in another
table.

When any value of primary key field is changed, the Foreign key in the
corresponding table is also changed
FOREIGN KEY
CREATE TABLE Orders
( O_Id NUMBER NOT NULL,
Order No NUMBER NOT NULL,
Product_Id NUMBER(10),
FOREIGN KEY (Product_Id) REFERENCES Persons(P_Id) )
or
ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
Now The "P_Id" column in the "Persons" table is the PRIMARY KEY in the
"Persons" table.
The "Product_Id" column in the "Orders" table is a FOREIGN KEY in the
"Orders" table.
How to delete a CONSTRAINT
ALTER TABLE Persons
DROP CONSTRAINT pc_PersonID
or
ALTER TABLE Persons
DROP PRIMARY KEY
ALTER TABLE to RENAME
Renaming a table:
ALTER TABLE table_name
RENAME TO new_table_name;
Example:
ALTER TABLE employees
RENAME TO EMP;
Rename column(s) in a table:
ALTER TABLE table_name
RENAME COLUMN old_name to new_name;
Example:
ALTER TABLE employees
RENAME COLUMN Emp_ID to EMP_NO;
ALTER TABLE to add column
Adding a column to a table:
ALTER TABLE table_name
ADD column_name column-definition;
Example:
ALTER TABLE employees
ADD age number(2);
To add multiple columns to an existing table:
ALTER TABLE table_name
ADD ( column_1 column-definition, column_2 column-definition, ... column_n
column_definition );
Example:
ALTER TABLE employees
ADD ( age number(2),
project varchar2(25) );
ALTER TABLE to modify table
To modify a column in an existing table:
ALTER TABLE table_name
MODIFY column_name column_type;
Example:
ALTER TABLE employees
MODIFY project varchar2(50)
Similarly multiple columns can be modified in a table
The modified syntax will be applicable only to the new rows
ALTER TABLE to drop column
Drop column(s) in a table:
ALTER TABLE table_name
DROP COLUMN column_name;
Example:
ALTER TABLE employees
DROP COLUMN project;

DROP TABLE
DROP TABLE table_name;
Example:
DROP TABLE employees;
It cannot be rolled back as it is a DDL statement.
VIEWS
Creating / Updating View
CREATE OR REPLACE VIEW emp1 AS
(SELECT e.Emp_ID,e.First_name,e.Last_name,d.Dept_ID,d.Salary
FROM employees e,departments d
WHERE e.Emp_ID = d.Emp_ID AND d.Salary >= 40000);

Select * from emp1;


Dropping View:
DROP VIEW emp1
UNION and UNION ALL
Syntax:
SELECT column_name(s) FROM table_name1
UNION / UNION ALL
SELECT column_name(s) FROM table_name2
Example:
Select Emp_ID from employees
UNION
Select Emp_ID from employee_details;
Note:

The UNION removes duplicates

Each SQL statement within the UNION query must have the same number of
fields in the result sets with similar data types

The UNION ALL displays duplicate values also.
INTERSECT
Syntax:
SELECT column_name(s) FROM table_name1
INTERSECT
SELECT column_name(s) FROM table_name2
Example:
Select Emp_ID from employees
INTERSECT
Select Emp_ID from employee_details;
Note:

Each SQL statement within the INTERSECT query must have the same
number of fields in the result sets with similar data type
MINUS
Syntax:
SELECT column_name(s) FROM table_name1
MINUS
SELECT column_name(s) FROM table_name2
Example:
Select Emp_ID from employees
MINUS
Select Emp_ID from employee_details;
Note:

Each SQL statement within the MINUS query must have the same number of
fields in the result sets with similar data type
NATURAL JOIN
Syntax:
SELECT column_name(s) FROM table_name1
NATURAL JOIN table_name2
Example:
Select Emp_ID,Address,First_name,d.Age,d.Skills from employees
NATURAL JOIN employee_details d ;
All the columns of one table can be joined to all columns of another table. Few columns
of one table can be joined to few/all columns of another table
Natural join combines two tables based on their common columns i.e. columns with the
same name. Therefore join condition is hidden
Natural join syntax is dangerous and may lead to unpleasant surprises because of zero
common columns between joined tables or too much common columns between
joined tables. Therefore better never use it in real production program code.
Here Emp_ID and Address field of both table is compared and matching results are
displayed.
JOIN or INNER JOIN
Syntax:
SELECT column_name(s) FROM table_name1
JOIN/INNER JOIN table_name2 using(field name)
Or
SELECT column_name(s) FROM table_name1
JOIN/INNER JOIN table_name2 ON <join_condition>
Qualifier should not be used in using field
The “employee_details” table has the old address of employees. The
“employee” table has updated address of employees. So we need to view
both the old address and the updated address of “employee_details” table.
How?
Select Emp_ID,First_name,Address “old”,d.Address “new”,d.Age, d.Skills
from employees JOIN employee_details d USING(Emp_ID);
APPENDIX-A:EMPLOYEES TABLE

Emp_ID First_name Last_name Address City Experience

332197 Nithya Kandappan pallikaranai Chennai 2

333118 Gayathri P Baby Nagar Chennai 2

471487 Thejas Patel Nandambak Chennai 1


kam

472256 Ramya Reddy velachery Chennai 1

501233 Shiva Kumar xxx Hyderabad 5

471244 Thejas Rajeev yyy Hyderabad 4


APPENDIX-B:EMPLOYEE_DETAILS
TABLE

Emp_I Age Sex Skills Position DOJ Address


D

332197 24 Female Windows Systems 12/14/09 pallikaranai


Engineer

471487 22 Male Solaris Trainee 02/28/10 Nandambakkam

472256 22 Female Websphere Trainee 02/21/10 velachery

471244 35 Male MB IT Analyst 06/06/07 Guindy


APPENDIX-C:DEPARTMENTS TABLE
Emp_ID First_name Last_name Department Dept_ID Salary

332197 Nithya Kandappan Marketing 22 45000

333118 Gayathri P HR 22 40000

471487 Thejas Patel Support 36 30000

472256 Ramya Reddy Marketing 36 35000

501233 Shiva Kumar HR 22 60000

471244 Thejas Rajeev Support 36 45000

You might also like