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

St. Xavier'S College: (Affiliated To Tribhuvan University) Maitighar, Kathmandu

1. Find product names and vendors for orders by customer 103 using a join. Find the same using a nested query filtering on the order and customer numbers. 2. Find customer names who ordered between two dates with a status of "Shipped" using a join. Find the same using a nested query filtering on the date range and status. 3. Find customer names and credit limits for customers with specific order numbers using a join and OR clauses. Find the same using a nested query filtering on the order numbers.

Uploaded by

Nilima Nayak
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

St. Xavier'S College: (Affiliated To Tribhuvan University) Maitighar, Kathmandu

1. Find product names and vendors for orders by customer 103 using a join. Find the same using a nested query filtering on the order and customer numbers. 2. Find customer names who ordered between two dates with a status of "Shipped" using a join. Find the same using a nested query filtering on the date range and status. 3. Find customer names and credit limits for customers with specific order numbers using a join and OR clauses. Find the same using a nested query filtering on the order numbers.

Uploaded by

Nilima Nayak
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

ST.

XAVIER’S COLLEGE
(Affiliated to Tribhuvan University)
Maitighar, Kathmandu

Database Management System 


Lab Assignment #4

Submitted By
Nilima Nayak
2nd Year/4th Sem
017BSCIT027

Submitted To
Signature Remarks

Er. Sarjan Shrestha


Lecturer, Dept. of Computer Science
LAB 4
SQL JOIN OPERATION
OBJECTIVES: TO UNDERSTAND ABOUT JOIN OPERATION AND
NESTED QUERIES

THEORY:

JOIN OPERATATION
The SQL joins clause is used to combine records from two or more tables in a database. A
JOIN is a means for combining fields from two tables by using values common to each.
Several operators can be used to join tables, such as =, <, >, < >, >=, <=, !=, BETWEEN,
LIKE, and NOT; they can all be used to join tables. However, the most common operator is
the equal to symbol.
There are different types of joins available in SQL −

 INNER JOIN − returns rows when there is a match in both tables.


 LEFT JOIN − returns all rows from the left table, even if there are no
 matches in the right table.
 RIGHT JOIN − returns all rows from the right table, even if there are no
 matches in the left table.
 FULL JOIN − returns rows when there is a match in one of the tables.
 SELF JOIN − is used to join a table to itself as if the table were two tables,
 temporarily renaming at least one table in the SQL statement.
 CARTESIAN JOIN − returns the Cartesian product of the sets of records from the
 two or more joined tables.

Syntax: SELECT table1_column_name(s), table1_column_name(s)


FROM table_name1, table_name2
WHERE common_column_name(table1) = common_column_name(table2)
NESTED QUERIES
In nested queries, a query is written inside a query. The result of inner query is used in
execution of outer query. 
There are mainly two types of nested queries:
1. Independent Nested Queries: In independent nested queries, query execution starts
from innermost query to outermost queries. The execution of inner query is
independent of outer query, but the result of inner query is used in execution of outer
query. Various operators like IN, NOT IN, ANY, ALL are used in writing
independent nested queries.
2. Co-related Nested Queries: In co-related nested queries, the output of inner query
depends on the row which is being currently executed in outer query. 

Syntax: SELECT column_name [, column_name ]


FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE]);

1. Find the product name and product vendor information for orders
made by customer number 103.
CREATE DATABASE lab4;
USE lab4;
SHOW TABLES;
SELECT DATABASE();

SELECT p.productName, p.productVendor


FROM products p, orderdetails od, orders o
WHERE o.orderNumber=od.orderNumber AND od.productCode=p.productCode
AND o.customerNumber=103;
USING NESTED QUERY: 

select productName, productVendor from products 


where productCode in (select productcode from orderdetails where
orderNumber in( SELECT orderNumber from orders where customerNumber
= 103));

2. Find the name of customers who ordered items between ‘2003-10-01’


and ‘2003-10-30’ and status of item is ‘Shipped’.
SELECT c.customerName 
FROM customers c, orders o
WHERE c.customerNumber=o.customerNumber
AND o.orderDate BETWEEN "2003-10-01" AND "2003-10-30"
AND o.status = "Shipped";

USING NESTED QUERY: 

select customerName from customers where customerNumber in (select


customerNumber from orders where (orderDate BETWEEN "2003-10-01" AND
"2003-10-30")
AND status = "Shipped");

3. Find the customer name and credit limit of the customers who made
orders with order numbers in 10128, 10130, 10136, 10137.
SELECT c.customerName, c.creditLimit
FROM customers c, orders o
WHERE c.customerNumber = o.customerNumber
AND o.orderNumber = 10128 OR 10130 OR 10136 OR 10137;

USING NESTED QUERY: 

select customerName, creditLimit from customers where customerNumber


in (select customerNumber from orders where orderNumber = 10128 OR
10130 OR 10136 OR 10137);

4. List the name of employees who are located at ‘Sydney’ Office.


SELECT CONCAT (e.firstName," ",e.lastName) AS "Employee Name"
FROM employees e, offices o
WHERE o.officeCode = e.officeCode
AND o.city = "Sydney";

USING NESTED QUERY: 

SELECT CONCAT (firstName," ",lastName) AS "Employee Name" FROM


employees where officeCode = (select officeCode from offices where
city = "Sydney");

5. List all the product details for the orders made by Diego Freyre
( contactFirstName contactLastName).
SELECT p.*
FROM products p, orders o, orderdetails od, customers c
WHERE c.customerNumber=o.customerNumber AND
o.orderNumber=od.orderNumber AND od.productCode=p.productCode
AND c.contactFirstName = "Diego" AND c.contactLastName = "Freyre";

USING NESTED QUERY: 

SELECT * FROM products WHERE productCode in(select productCode from


orderdetails where orderNumber in
(select orderNumber from orders where customerNumber =
(select customerNumber from customers where contactFirstName =
"Diego" AND contactLastName = "Freyre")));

6. List firstName, lastName and email of employee who is located in


office at ‘San Francisco’.
SELECT firstName, lastName, email from employees where officeCode =
( SELECT officeCode from offices where city = 'San Francisco');

7. Find the details of employees who reports to ‘Diane Murphy’.


SELECT * FROM employees
where reportsTo = ( Select employeeNumber from employees where
firstName = "Diane" and lastName="Murphy");

8. Find the product name and productline that are contained in order
number 10100.
SELECT productName, productLine from products 
where productCode in (Select productCode from orderdetails where
orderNumber = 10100);

You might also like