0% found this document useful (0 votes)
4 views32 pages

Subjoints, Constraints

The document contains SQL table creation scripts, including primary and foreign key constraints, as well as examples of subqueries and various types of SQL joins. It provides sample data for tables like 'Salesman' and 'Orders' and demonstrates how to query these tables using SQL syntax. Additionally, it explains the concept of self-joins and the use of aliases in SQL queries.

Uploaded by

chitrammakers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views32 pages

Subjoints, Constraints

The document contains SQL table creation scripts, including primary and foreign key constraints, as well as examples of subqueries and various types of SQL joins. It provides sample data for tables like 'Salesman' and 'Orders' and demonstrates how to query these tables using SQL syntax. Additionally, it explains the concept of self-joins and the use of aliases in SQL queries.

Uploaded by

chitrammakers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Primary Key

CREATE TABLE Persons (


PersonID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (PersonID)
);
-- add the PRIMARY KEY constraint to multiple columns
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT PK_Person PRIMARY KEY (ID,
LastName)
);
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
Exercise
CREATE TABLE customer1 (
cust_code CHAR(6) NOT NULL PRIMARY KEY,
cust_name CHAR(25),
cust_city CHAR(25),
agent_code char(6),
FOREIGN KEY(agent_code) REFERENCES agent1(agent_code)
);
-- this table doesn't contain foreign keys
CREATE TABLE Customers (
id INTEGER PRIMARY KEY,
name VARCHAR(100),
age INTEGER
);

-- create another table named Products


-- add foreign key to the customer_id column
-- the foreign key references the id column of the Customers table

CREATE TABLE Products (


customer_id INTEGER ,
name VARCHAR(100),
FOREIGN KEY (customer_id)
REFERENCES Customers(id)
);
Sub Queries
• A subquery is nothing more than a query inside another
query.
-- use a subquery to select the first name of customer
-- with the highest age

SELECT first_name
FROM Customers
WHERE age= (
SELECT MAX(age)
FROM CUSTOMERS
);
CREATE TABLE Companies (
id int,
name varchar(50),
address text,
email varchar(50),
phone varchar(10)
);
Sample table: Salesman

salesman_id name city commission


----------- ---------- ---------- ----------
• 5001 James Hoog New York 0.15
• 5002 Nail Knite Paris 0.13
• 5005 Pit Alex London 0.11
• 5006 Mc Lyon Paris 0.14
• 5003 Lauson Hen San Jose 0.12
• 5007 Paul Adam Rome 0.13
Sample table: Orders

• ord_no purch_amt ord_date customer_id salesman_id


• ---------- ---------- ---------- ----------- -----------
• 70001 150.5 2012-10-05 3005 5002
• 70009 270.65 2012-09-10 3001 5005
• 70002 65.26 2012-10-05 3002 5001
• 70004 110.5 2012-08-17 3009 5003
• 70007 948.5 2012-09-10 3005 5002
• 70005 2400.6 2012-07-27 3007 5001
• 70008 5760 2012-09-10 3002 5001
• 70010 1983.43 2012-10-10 3004 5006
• 70003 2480.4 2012-10-10 3009 5003
• 70012 250.45 2012-06-27 3008 5002
• 70011 75.29 2012-08-17 3003 5007
• 70013 3045.6 2012-04-25 3002 5001
write a SQL query to find all the orders issued by the salesman
'Paul Adam'. Return ord_no, purch_amt, ord_date, customer_id
and salesman_id.

SELECT *
FROM orders
WHERE salesman_id =
(SELECT salesman_id
FROM salesman
WHERE name='Paul Adam');
• write a SQL query to find all orders generated by London-based salespeople.
Return ord_no, purch_amt, ord_date, customer_id, salesman_id.

SELECT *
FROM orders
WHERE salesman_id IN
(SELECT salesman_id
FROM salesman
WHERE city='London');
write a SQL query to find all orders generated by the salespeople
who may work for customers whose id is 3007. Return ord_no,
purch_amt, ord_date, customer_id, salesman_id.

SELECT *
FROM orders
WHERE salesman_id =
(SELECT DISTINCT salesman_id
FROM orders
WHERE customer_id = 3007);
Sub query
CREATE TABLE employees (
employee_id INT (11) AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR (20) DEFAULT NULL,
last_name VARCHAR (25) NOT NULL,
email VARCHAR (100) NOT NULL,
phone_number VARCHAR (20) DEFAULT NULL,
hire_date DATE NOT NULL,
job_id INT (11) NOT NULL,
salary DECIMAL (8, 2) NOT NULL,
manager_id INT (11) DEFAULT NULL,
department_id INT (11) DEFAULT NULL,
FOREIGN KEY (job_id) REFERENCES jobs (job_id) ON DELETE CASCADE ON UPDATE
CASCADE,
FOREIGN KEY (department_id) REFERENCES departments (department_id) ON DELETE
CASCADE ON UPDATE CASCADE,
FOREIGN KEY (manager_id) REFERENCES employees (employee_id)
);
CREATE TABLE departments (
department_id INT (11) AUTO_INCREMENT PRIMARY KEY,
department_name VARCHAR (30) NOT NULL,
location_id INT (11) DEFAULT NULL,
FOREIGN KEY (location_id) REFERENCES locations (location_id)
ON DELETE CASCADE ON UPDATE CASCADE
);
• find all departments Second, find all employees that
belong to the location 1700 by using
located at the location the department id list of the previous
whose id is 1700: query:
SELECT
SELECT employee_id, first_name, last_name
FROM
* employees
WHERE
FROM department_id IN (1 , 3, 8, 10, 11)
ORDER BY first_name , last_name;
departments SELECT
employee_id, first_name, last_name
WHERE FROM
employees
location_id = 1700; WHERE
department_id IN (SELECT
department_id
FROM
departments
WHERE
location_id = 1700)
ORDER BY first_name , last_name;
find all employees who do not locate at the location 1700:
SELECT
employee_id, first_name, last_name
FROM
employees
WHERE
department_id NOT IN (SELECT
department_id
FROM
departments
WHERE
location_id = 1700)
ORDER BY first_name , last_name;
SQL Joins
A JOIN clause is used to combine rows from two or more tables,
based on a related column between them.

Different Types of SQL JOINs

(INNER) JOIN: Returns records that have matching values in both tables

LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table

RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table

FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
Inner Join

SELECT ProductID, ProductName, CategoryName


FROM Products
INNER JOIN Categories ON Products.CategoryI
D = Categories.CategoryID;
Left Outer Join
Left Join The LEFT JOIN keyword returns all records from the left table (table1), and
the matching records from the right table (table2). The result is 0 records
from the right side, if there is no match.

• SELECT Customers.CustomerName, Orders.OrderID


• FROM Customers
• LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
• ORDER BY Customers.CustomerName;
Right Join

• The RIGHT JOIN keyword returns all records from the right table
(table2), and the matching records from the left table (table1). The
result is 0 records from the left side, if there is no match.
• SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
• FROM Orders
• RIGHT JOIN Employees ON Orders.EmployeeID =
Employees.EmployeeID
• ORDER BY Orders.OrderID;
FULL OUTER JOIN
• The FULL OUTER JOIN keyword returns all records when there is a
match in left (table1) or right (table2) table records.
• SELECT Customers.CustomerName, Orders.OrderID
• FROM Customers
• FULL OUTER JOIN Orders ON
Customers.CustomerID=Orders.CustomerID
• ORDER BY Customers.CustomerName;
Self join

• Sometimes, it is useful to join a table to itself. This type of join is


known as the self-join.
• To perform the self-join, we use either an inner join or left join clause.

SELECT e.first_name || ' ' || e.last_name AS employee,


m.first_name || ' ' || m.last_name AS manager FROM employees
e INNER JOIN employees m ON m.employee_id = e.manager_id
ORDER BY manager;
Alias
SELECT
inv_no AS invoice_no,
amount,
due_date AS 'Due date',
cust_no 'Customer No'
FROM
invoices;

You might also like