Subjoints, Constraints
Subjoints, Constraints
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
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.
(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
• 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