Task - Joins - classicmodels DB #3
Replies: 41 comments
-- Task 14 march 2022 Morning session
-- Qusetion 1:
SELECT c.city , c.customerName, m.customerName FROM customers c INNER JOIN customers m
ON c.city = m.city WHERE c.customerName<>m.customerName ORDER BY c.city;
-- Qusetion 2:
SELECT p.productCode , p.ProductName , l.textDescription FROM products p INNER JOIN productlines l
ON p.productline = l.productline;
-- Qusetion 3:
SELECT o.orderNumber , o.status , SUM(priceEach*quantityOrdered) FROM orders o INNER JOIN orderdetails od
ON o.orderNumber=od.orderNumber GROUP BY od.orderNumber;
-- Qusetion 4:
SELECT o.orderNumber,o.orderDate, od.orderLineNumber, p.productName, od.quantityOrdered,od.priceEach FROM orders o
INNER JOIN orderdetails od ON o.orderNumber=od.orderNumber
INNER JOIN products p ON od.productCode=p.productCode
ORDER BY orderNumber,orderLineNumber;
-- Qusetion 5:
SELECT o.orderNumber,o.orderdate,c.customerName,od.orderLineNumber,p.productName, od.quantityOrdered,od.priceEach FROM orders o
INNER JOIN customers c ON o.customerNumber=c.customerNumber
INNER JOIN orderdetails od ON o.orderNumber=od.orderNumber
INNER JOIN products p ON od.productCode=p.productCode
ORDER BY orderNumber,orderLineNumber;
-- Qusetion 6:
SELECT od.orderNumber, p.productName, msrp, priceEach FROM orderdetails od
INNER JOIN products p ON od.productCode=p.productCode
WHERE p.productCode = 'S10_1678' AND priceEach<msrp;
-- Qusetion 7:
SELECT c.customerNumber, c.customerName, o.ordernumber, o.status FROM customers c
LEFT JOIN orders o ON c.customerNumber=o.customerNumber;
-- Qusetion 8:
SELECT c.customerNumber, c.customerName, o.ordernumber, o.status FROM customers c
LEFT JOIN orders o ON c.customerNumber=o.customerNumber AND o.ordernumber IS NULL;
|
1. select a.city as city,a.customerName as customerName,b.customerName as customerName from customers a,customers b where a.customerNumber <> b.customerNumber AND a.city=b.city order by a.city;
2.select productCode,productName,textDescription from products a inner join productlines b on a.productline=b.productline;
3.select a.orderNumber,status,SUM(quantityOrdered * priceEach) as total from orders a inner join orderdetails b on a.orderNumber=b.orderNumber group by a.orderNumber;
4.select orderNumber,orderDate,orderLineNumber,productName,quantityOrdered,priceEach from orders inner join orderdetails using (orderNumber) inner join products using (productCode) order by orderNumber,orderLineNumber;
5.select orderNumber,orderDate,customerName,orderLineNumber,productName,quantityOrdered,priceEach from orders inner join orderdetails using (orderNumber) inner join products using (productCode) inner join customers using(customerNumber) order by orderNumber,orderLineNumber;
6.select orderNumber,productName,msrp,priceEach from products a inner join orderdetails b on a.productcode=b.productcode and a.msrp>b.priceEach where a.productCode='S10_1678';
7.select a.customerNumber,a.customerName,b.orderNumber,b.status from customers a inner join orders b on a.customerNumber=b.customerNumber order by a.customerNumber;
8.select a.customerNumber,a.customerName,b.orderNumber,b.status from customers a left join orders b on a.customernumber=b.customernumber and b.ordernumber is null; |
|
|
|
|
|
1.Write a query to display a list of customers who locate in the same city by joining the customers table to itself.
2.write queries to get:
(ii).The textDescription of product lines from the productlines table.
3.Write a query that returns order number, order status, and total sales from the orders and orderdetails tables as follows:
4.Write a query to fetch the complete details of orders from the orders, orderDetails, and products table, and sort them by orderNumber and orderLineNumber as follows: 5.Write a query to perform INNER JOIN of four tables: 7.Each customer can have zero or more orders while each order must belong to one customer. Write a query to find all the customers and their orders as follows: 8.Write a query that uses the LEFT JOIN to find customers who have no order: |
|
6.select ordet.orderNumber,p.productName,p.msrp, ordet.priceEach from products p inner join orderdetails ordet on p.productCode = ordet.productCode and ordet.priceEach < p.msrp where ordet.productCode="S10_1678" limit 8;
8.SELECT c.customerNumber, c.customerName, o.ordernumber, o.status FROM customers c LEFT JOIN orders o ON c.customerNumber=o.customerNumber where orderNumber is NULL; |
|
1.SELECT c.city , c.customerName, a.customerName FROM customers c INNER JOIN customers a
ON c.city = a.city WHERE c.customerName<>a.customerName ORDER BY c.city;
2.SELECT productCode,productName,textDescription from products p inner join productlines pl on p.productline=pl.productline;
3.SELECT o.orderNumber,o.status,SUM(quantityOrdered * priceEach) as total from orders o inner join orderdetails od on o.orderNumber=od.orderNumber group by o.orderNumber;
4.SELECT orderNumber,orderDate,orderLineNumber,productName,quantityOrdered,priceEach from orders inner join orderdetails using (orderNumber) inner join products using (productCode) order by orderNumber,orderLineNumber;
5.SELECT orderNumber,orderDate,customerName,orderLineNumber,productName,quantityOrdered,priceEach from orders inner join orderdetails using (orderNumber) inner join products using (productCode) inner join customers using(customerNumber) order by orderNumber,orderLineNumber;
6.SELECT orderNumber,productName,msrp,priceEach from products p inner join orderdetails od on p.productcode=od.productcode and p.msrp>od.priceEach where p.productCode='S10_1678';
7.SELECT customerNumber,customerName,orderNumber,status from customers left join orders using(customerNumber) ;
8.SELECT c.customerNumber,c.customerName,o.orderNumber,o.status from customers c left join orders o on c.customernumber=o.customernumber and o.ordernumber is null; |
|
|
|
|
1. select c.city,c.customerName, c1.customerName
from customers c inner join customers c1 using(city)
where(c.customerName<> c1.customerName)
order by city;
2. select p.productCode, p.productName , pl.textDescription
from products p natural join productlines pl;
3. select o.orderNumber, o.status,
sum(d.priceEach*d.quantityOrdered) as total
from orders o natural join orderdetails d
group by o.orderNumber;
4. SELECT o.orderNumber,o.orderdate,d.orderLineNumber,
p.productname,d.quantityordered,d.priceeach
from orders o inner join orderDetails d
USING(orderNumber) inner join products p
using(productCode) ORDER BY o.orderNumber,d.orderLineNumber;
5. SELECT o.orderNumber, o.orderDate,
c.customerName,d.orderLineNumbers,p.productName,
d.quantityOrdered,d.priceEach from orders o
inner join customers c using(customernumber)
inner join orderdetails d using(ordernumber)
inner join products p USING(productCode)
ORDER BY orderNumber,orderLineNumber;
6. SELECT d.orderNumber, p.productName, p.MSRP,
d.priceEach from orderdetails d inner join
products p using(productCode)
WHERE d.productCode = 'S10_1678'
AND p.MSRP>d.priceEach;
7. c.customerNumber,c.customerName,
o.orderNumber,o.status
from customers c left join orders o
using(customerNumber);
8. select c.customerNumber,c.customerName,
o.orderNumber,o.status
from customers c left join orders o
using(customerNumber)
where o.orderNumber is null;
|
|
use classicmodels;
-- 1st issue
select c1.city, c1.customerName, c2.customerName
from customers c1
inner join customers c2
on c1.city = c2.city and c1.customerName <> c2.customerName
order by c1.city;
-- 2nd
select p1.productCode, p1.productName, p2.textDescription
from products p1
join productlines p2
on p1.productLine = p2.productLine;
-- 3rd
select o.orderNumber, o.status, sum(o2.quantityOrdered*o2.priceEach) as total
from orders o
inner join orderdetails o2
on o.orderNumber = o2.orderNumber
group by o.orderNumber;
-- 4th
select o1.orderNumber, o1.orderDate, o2.orderLineNumber, p.productName, o2.quantityOrdered, o2.priceEach
from orders o1
join orderdetails o2 on o1.orderNumber = o2.orderNumber
join products p on p.productCode = o2.productCode
order by o1.orderNumber,o2.orderLineNumber;
-- 5th
select o1.orderNumber, o1.orderDate, c.customerName, o2.orderLineNumber, p.productName, o2.quantityOrdered, o2.priceEach
from orders o1
join orderdetails o2
on o1.orderNumber = o2.orderNumber
join products p
on p.productCode = o2.productCode
join customers c
on c.customerNumber = o1.customerNumber
order by o1.orderNumber,o2.orderLineNumber;
-- 6th
select o1.orderNumber, p.productName, p.msrp, o2.priceEach
from orders o1
join orderdetails o2
on o1.orderNumber = o2.orderNumber
join products p
on p.productCode = o2.productCode
where p.productCode = 'S10_1678' and p.msrp > o2.priceEach;
-- 7th
select c.customerNumber, c.customerName, o.orderNumber, o.status
from customers c
inner join orders o
on c.customerNumber = o.customerNumber
order by o.orderNumber;
-- 8th
select c.customerNumber, c.customerName, o.orderNumber, o.status
from customers c
left join orders o
on c.customerNumber = o.customerNumber
where o.orderNumber is null and o.status is null; |
SELECT c.city , c.customerName , m.customerName FROM customers c INNER JOIN customers m
ON c.city = m.city where c.customerName<> m.customerName order by c.city;SELECT p.productCode,p.productName, d.textDescription FROM products p JOIN productlines d
WHERE p.productLine=d.productLine;select o.orderNumber , o.status, sum(quantityOrdered*priceEach) as total from orders o inner join orderdetails od on o.orderNumber=od.orderNumber
group by od.orderNumber;select o.orderNumber,o.orderDate,od.orderLineNumber ,p.productName,od.quantityOrdered,od.priceEach from orders o INNER JOIN orderdetails od on
o.orderNumber=od.orderNumber INNER JOIN products p on od.productCode=p.productCode
order by orderNumber,orderLineNumber;SELECT o.orderNumber,o.orderdate,c.customerName,od.orderLineNumber,p.productName, od.quantityOrdered,od.priceEach FROM orders o
INNER JOIN customers c ON o.customerNumber=c.customerNumber
INNER JOIN orderdetails od ON o.orderNumber=od.orderNumber
INNER JOIN products p ON od.productCode=p.productCode
ORDER BY orderNumber,orderLineNumber;SELECT od.orderNumber, p.productName, msrp, priceEach FROM orderdetails od
INNER JOIN products p ON od.productCode=p.productCode
WHERE p.productCode = 'S10_1678' AND priceEach<msrp;SELECT c.customernumber,c.customername,o.ordernumber,o.status FROM customers c
LEFT JOIN orders o ON c.customernumber=o.customernumber ORDER BY c.customernumber ;SELECT c.customernumber,c.customername,o.ordernumber,o.status FROM customers c
LEFT JOIN orders o ON c.customernumber=o.customernumber WHERE o.ordernumber is null;
|
|
1. select c1.city,c1.customerName,c2.customerName from customers c1 inner join customers c2 using(city) where c1.customerName>c2.customerName order by c1.city;
2. select p.productCode,p.productName,pl.textDescription from products p natural join productLines pl;
3. select orderNumber,status,sum(quantityOrdered*priceEach) as total from orders inner join orderdetails using (orderNumber) group by orderNumber ;
4. select orderNumber,orderDate,orderLineNumber,productName,quantityOrdered,priceEach from orders inner join orderdetails using(orderNumber) inner join products using(productCode) order by orderNumber,orderLineNumber ;
5. select orderNumber,orderDate,customerName,orderLineNumber,productName,quantityOrdered,priceEach from customers inner join orders using(customerNumber) inner join orderdetails using(orderNumber) inner join products using(productCode) order by orderNumber,orderLineNumber ;
6. select orderNumber,productName,msrp,priceEach from products inner join orderdetails using(productCode) where productCode="S10_1678" and msrp>priceEach;
7. select customerNumber,customerName,orderNumber,status from customers left join orders using(customerNumber) ;
8. select customerNumber,customerName,orderNumber,status from customers left join orders using(customerNumber) where orderNumber is NULL;
|
--Q1
select a.city,
a.customerName,
b.customerName
from customers a,
customers b
where a.city = b.city
AND a.customerName <> b.customerName
order by a.city;
--Q2
select p.productCode,
p.productName,
l.textDescription
from products p,
productlines l
where p.productline = l.productline;
--Q3
select d.orderNumber,
o.status,
sum(d.priceEach * d.quantityOrdered) as `Total`
from orderdetails d,
orders o
where d.orderNumber = o.orderNumber
group by orderNumber;
--Q4
select o.orderNumber,
o.orderDate,
c.customerName,
d.orderLineNumber,
p.productName,
d.quantityOrdered,
d.priceEach
from customers c,
products p,
orders o,
orderdetails d
where c.customerNumber = o.customerNumber
AND o.orderNumber = d.orderNumber
AND d.productCode = p.productCode
ORDER BY o.orderNumber,
d.orderLineNumber;
--Q5
select o.orderNumber,
o.orderDate,
c.customerName,
d.orderLineNumber,
p.productName,
d.quantityOrdered,
d.priceEach
from customers c,
orders o,
orderdetails d,
products p
where o.orderNumber = d.orderNumber
and d.productCode = p.productCode
and o.customerNumber = c.customerNumber
order by o.orderNumber,
d.orderLineNumber;
--Q6
select d.orderNumber,
p.productName,
p.MSRP,
d.priceEach
from orderdetails d,
products p
where p.productCode = 'S10_1678'
AND d.productCode = p.productCode
AND d.priceEach < p.msrp;
--Q7
select c.customerNumber,
c.customerName,
o.orderNumber,
o.status
from customers c
left join orders o on o.customerNumber = c.customerNumber;
--Q8
SELECT c.customerNumber,
c.customerName,
o.ordernumber,
o.status
FROM customers c
LEFT JOIN orders o ON c.customerNumber = o.customerNumber
AND o.ordernumber IS NULL; |
1. select c1.city,c1.customerName,c2.customerName from customers c1 inner join customers c2 using(city) where c1.customerName>c2.customerName order by c1.city;
2. select p.productCode,p.productName,pl.textDescription from products p natural join productLines pl;
3. select orderNumber,status,sum(quantityOrdered*priceEach) as total from orders inner join orderdetails using (orderNumber) group by orderNumber ;
4. select orderNumber,orderDate,orderLineNumber,productName,quantityOrdered,priceEach from orders inner join orderdetails using(orderNumber) inner join products using(productCode) order by orderNumber,orderLineNumber ;
5. select orderNumber,orderDate,customerName,orderLineNumber,productName,quantityOrdered,priceEach from customers inner join orders using(customerNumber) inner join orderdetails using(orderNumber) inner join products using(productCode) order by orderNumber,orderLineNumber ;
6. select orderNumber,productName,msrp,priceEach from products inner join orderdetails using(productCode) where productCode="S10_1677" and msrp>priceEach;
7. select customerNumber,customerName,orderNumber,status from customers left join orders using(customerNumber) ;
8. select customerNumber, customerName, orderNumber, status from customers left join orders using(customerNumber) where orderNumber is NULL; |
select c1.city, c1.customerName, c2.customerName from customers c1 inner join customers c2 where c1.city=c2.city and c1.customerName<> c2.customerName order by city;
select productCode, productName, textDescription from products inner join productlines using(productLine);
select orderNumber, status, sum(priceEach*quantityOrdered)as total from orderdetails inner join orders using(orderNumber) group by orderNumber;
select * from orders inner join orderdetails using (orderNumber) inner join products using(productCode) order by orderNumber,orderLineNumber;
select * from customers inner join orders using(customerNumber) inner join orderdetails using (orderNumber) inner join products using(productCode) order by orderNumber, orderLineNumber;
SELECT od.orderNumber, p.productName, msrp, priceEach FROM orderdetails od INNER JOIN products p ON od.productCode=p.productCode WHERE p.productCode = 'S10_1678' AND priceEach<msrp;
SELECT c.customerNumber, c.customerName, o.ordernumber, o.status FROM customers c LEFT JOIN orders o ON c.customerNumber=o.customerNumber;
SELECT c.customerNumber, c.customerName, o.ordernumber, o.status FROM customers c LEFT JOIN orders o ON c.customerNumber=o.customerNumber AND o.ordernumber IS NULL; |
|
use classicmodels; --2 --3 --4 --5 --6 --7 --8 |
|
-- 1. Write a query to display a list of customers who locate in the same city by joining the customers table to itself. select c1.city,c1.customerName as customer_from_c1 ,c2.customerName as customer_from_c2 --2. Write a query to get: select p1.productCode,p1.productName,p2.textDescription --3 Write a query that returns order number, order status, and total sales from the orders and orderdetails tables as follows: select od.orderNumber,o.status,(od.priceEach* od.quantityOrdered) as total --4 Write a query to fetch the complete details of orders from the orders, orderDetails, and products table, and sort them by orderNumber and orderLineNumber as follows: select o.orderNumber,o.orderDate,od.orderLineNumber,p.productName,od.quantityOrdered,od.priceEach --5 Write a query to perform INNER JOIN of four tables: select o.orderNumber,o.orderDate,c.customerName,od.orderLineNumber,p.productName,od.quantityOrdered,od.priceEach --6 Write a query to find the sales price of the product whose code is S10_1678 that is less than the manufacturer’s suggested retail price (MSRP) for that product as follows: select o.orderNumber,p.productName,p.MSRP,od.priceEach --7 Each customer can have zero or more orders while each order must belong to one customer. Write a query to find all the customers and their orders as follows: select c.customerNumber,c.customerName,o.orderNumber,o.status --8 Write a query that uses the LEFT JOIN to find customers who have no order: select c.customerNumber,c.customerName,o.orderNumber,o.status |
|
#1. Write a query to display a list of customers who locate in the same city by joining the customers table to itself. SELECT c1.city,c1.customerName,c2.customerName FROM customers c1 #2. Write a query to get: SELECT p.productCode,p.productName,p2.textDescription FROM products p #3. Write a query that returns order number, order status, and total sales from #4. Write a query to fetch the complete details of orders from the orders, orderDetails, and products table, and sort #5. Write a query to perform INNER JOIN of four tables: SELECT o2.orderNumber,o2.orderDate,o.orderLineNumber,p.productName,o.quantityOrdered,o.priceEach FROM products p #6. Write a query to find the sales price of the product whose code is S10_1678 that is less SELECT o.orderNumber,p.productName,p.MSRP,o.priceEach FROM orderdetails o #7 - Each customer can have zero or more orders while each order must belong to one customer. #8. Write a query that uses the LEFT JOIN to find customers who have no order: |
-- 1. Write a query to display a list of customers who locate in the same city by joining the customers table to itself.
select c1.city , c1.customerName, c2.customerName
from customers c1
join customers c2 using (city)
where c1.customerName < c2.customerName
order by c1.city;
-- 2. Write a query to get:
-- - The productCode and productName from the products table.
-- - The textDescription of product lines from the productlines table.
select productCode, productName, textDescription
from products
join productlines using (productline);
-- 3. Write a query that returns order number, order status, and total
-- sales from the orders and orderdetails tables.
select orderNumber, status, sum(quantityOrdered * priceEach) as total
from orders
join orderdetails using (orderNumber)
group by orderNumber, status;
-- 4. Write a query to fetch the complete details of orders from the orders,
-- orderDetails, and products table, and sort them by orderNumber and orderLineNumber.
select *
from products
join orderdetails using (productCode)
join orders using (orderNumber)
order by orderNumber, orderLineNumber;
-- 5. Write a query to perform INNER JOIN of four tables.
-- Display the details sorted by orderNumber, orderLineNumber.
select *
from orders
join orderdetails using (orderNumber)
join products using (productCode)
join productlines using (productline)
order by orderNumber, orderLineNumber;
-- 6. Write a query to find the sales price of the product whose code is S10_1678
-- ie less than the manufacturer’s suggested retail price (MSRP) for that product.
select orderNumber, productName, msrp, priceEach
from products
join orderdetails using (productCode)
where productCode = 'S10_1678' and priceEach < msrp;
-- 7. Each customer can have zero or more orders while each order must belong to
-- one customer. Write a query to find all the customers and their orders.
select customerNumber, customerName, orderNumber, status
from customers
left join orders using (customerNumber);
-- 8. Write a query that uses the LEFT JOIN to find customers who have no order.
select customerNumber, customerName, orderNumber, status
from customers
left join orders using (customerNumber)
where orderNumber is null; |
|
#1. Write a query to display a list of customers who locate in the same city by joining the customers table to itself. USE classicmodels; SELECT c1.city,c1.customerName,c2.customerName FROM customers c1 #2. Write a query to get: SELECT p.productCode,p.productName,p2.textDescription FROM products p #3. Write a query that returns order number, order status, and total sales from #4. Write a query to fetch the complete details of orders from the orders, orderDetails, and products table, and sort #5. Write a query to perform INNER JOIN of four tables: SELECT o2.orderNumber,o2.orderDate,o.orderLineNumber,p.productName,o.quantityOrdered,o.priceEach FROM products p #6. Write a query to find the sales price of the product whose code is S10_1678 that is less SELECT o.orderNumber,p.productName,p.MSRP,o.priceEach FROM orderdetails o #7 - Each customer can have zero or more orders while each order must belong to one customer. #8. Write a query that uses the LEFT JOIN to find customers who have no order: |
|
#1q #2q #3q #4q #5q #6q #7q #8q |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Database =
classicmodelsDump file: classicmodels.sql
DB Schema
Queries
Write a query to display a list of customers who locate in the same city by joining the

customerstable to itself.Write a query to get:
productCodeand productName from theproductstable.textDescriptionof product lines from theproductlinestable.Write a query that returns order number, order status, and total sales from the

ordersandorderdetailstables as follows:Write a query to fetch the complete details of orders from the

orders,orderDetails, andproductstable, and sort them by orderNumber and orderLineNumber as follows:Write a query to perform


INNER JOINof four tables:Display the details sorted by orderNumber, orderLineNumber as per the following
Write a query to find the sales price of the product whose code is S10_1678 that is less than the manufacturer’s suggested retail price (MSRP) for that product as follows:

Each customer can have zero or more orders while each order must belong to one customer. Write a query to find all the customers and their orders as follows:

Write a query that uses the

LEFT JOINto find customers who have no order:All reactions