0% found this document useful (0 votes)
37 views11 pages

Nishtha Assignment

The document contains a student's assignment on SQL queries. It includes 10 exercises with SQL code and explanations to retrieve information from database tables. The exercises select and join data from tables like ORDERS, CUSTOMER, and REP to filter rows by dates and credit limits and order results.

Uploaded by

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

Nishtha Assignment

The document contains a student's assignment on SQL queries. It includes 10 exercises with SQL code and explanations to retrieve information from database tables. The exercises select and join data from tables like ORDERS, CUSTOMER, and REP to filter rows by dates and credit limits and order results.

Uploaded by

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

ASSIGNMENT-8 (SQL DATABASE)

NAME – NISHTHA GAUR


STUDENT ID – C0912103
FOLLOWING THE GIVEN SCRIPT WE WILL CREATE, AND ALTER THE RECORDS AND TABLES.

CREATING TABLES AND ENTERING RECORDS

NOW WITH THE TABLE AND RECORD WE CAN START WITH


THE QUESTIONS.
1. For each order, list the order number and order date along with the
number and name of the customer that placed the order
CODE FOR THE FOLLOWING –
SELECT O.ORDER_NUM AS ORDER_NUM_103, O.ORDER_DATE, C.CUSTOMER_NUM
AS CUSTOMER_NUM, C.CUSTOMER_NAME
FROM ORDERS O, CUSTOMER C
WHERE O.CUSTOMER_NUM=C.CUSTOMER_NUM
ORDER BY C.CUSTOMER_NUM;

EXPLANATION 1:
This query retrieves information from the ORDERS and CUSTOMER tables, where
the customer numbers match, and it orders the results based on the customer
number in ascending order. Additionally, column aliases are used to rename
specific columns in the result set.
2. For each order placed on October 23, 2010, list the order number
along with the number and name of the customer that placed the order.

CODE FOR THE FOLLOWING

SELECT O.ORDER_NUM AS ORDER_NUM_103, O.ORDER_DATE, C.CUSTOMER_NUM


AS CUSTOMER_NUM, C.CUSTOMER_NAME
FROM ORDERS O, CUSTOMER C
WHERE O.CUSTOMER_NUM=C.CUSTOMER_NUM
AND O.ORDER_DATE='23-OCT-10'
ORDER BY C.CUSTOMER_NUM;

EXPLANATION 2:
This query retrieve information about orders placed on October 23, 2010 (O.ORDER_DATE
= '23-OCT-10'). It selects the order number, order date, customer number, and customer
name, and orders the result set based on the customer number
3. For each order, list the order number, order date, part number,
number of units ordered, and quoted price for each order line that
makes up the order.
THE CODE FOR FOLLOWING
SELECT O.ORDER_NUM AS ORDER_NUM_103,
O.ORDER_DATE, OL.PART_NUM, OL.NUM_ORDERED,
OL.QUOTED_PRICE
FROM ORDERS O, ORDER_LINE OL
WHERE O.ORDER_NUM=OL.ORDER_NUM;

EXPLANATION 3:
The query essentially retrieves information about order lines,
including the order number (renamed as ORDER_NUM_251),
order date, part number, number ordered, and quoted price.
The data is obtained by combining information from the
ORDERS and ORDER_LINE tables where the order numbers
match.
4. Use the IN operator to find the number and name of each customer
that placed an order on October 23, 2010
CODE FOR THE FOLLOWING
SELECT CUSTOMER_NUM, CUSTOMER_NAME FROM
CUSTOMER
WHERE CUSTOMER_NUM IN(SELECT CUSTOMER_NUM FROM
ORDERS
WHERE ORDER_DATE='23-OCT-10');

EXPLANATION 4 :
This query retrieve the customer numbers and names for customers who placed orders on
October 23, 2010. The IN clause is used to compare the CUSTOMER_NUM from the
CUSTOMER table with the result of the subquery, which returns customer numbers
associated with orders placed on the specified date.
5. Repeat Exercise 4, but this time use the EXISTS operator in your
answer
CODE FOR THE FOLLOWING

SELECT CUSTOMER_NUM, CUSTOMER_NAME FROM CUSTOMER C


WHERE EXISTS(SELECT CUSTOMER_NUM FROM ORDERS O
WHERE C.CUSTOMER_NUM=O.CUSTOMER_NUM
AND ORDER_DATE='23-OCT-10');

EXPLANATION 5:

This query retrieves the CUSTOMER_NUM and CUSTOMER_NAME from the CUSTOMER table for
customers who have placed an order on '23-OCT-10' (based on the existence of matching records in
the ORDERS table).
6. Find the number and name of each customer that did not place an
order on October 23, 2010.
CODE FOR FOLLOWING

SELECT CUSTOMER_NUM, CUSTOMER_NAME FROM CUSTOMER C


WHERE NOT EXISTS(SELECT CUSTOMER_NUM FROM ORDERS O
WHERE C.CUSTOMER_NUM=O.CUSTOMER_NUM
AND ORDER_DATE='23-OCT-10');

EXPLANATION 6:

The main query selects customers (CUSTOMER_NUM and CUSTOMER_NAME) from the CUSTOMER
table, excluding those customers who have placed an order on the specified date ('23-OCT-10') based
on the subquery using NOT EXISTS.
7. For each order, list the order number, order date, part number, part
description, and item class for each part that makes up the order
CODE FOR FOLLOWING

SELECT O.ORDER_NUM, O.ORDER_DATE, P.PART_NUM, P.DESCRIPTION,


P.CLASS
FROM ORDERS O, ORDER_LINE OL, PART P
WHERE O.ORDER_NUM=OL.ORDER_NUM
AND OL.PART_NUM=P.PART_NUM;

EXPLANATION 7:

The provided SQL query retrieves information about each order, including the order number
(ORDER_NUM) and order date (ORDER_DATE). It also includes details about the items in each order,
such as the part number (PART_NUM), part description (DESCRIPTION), and item class (CLASS). The
information is obtained by combining data from the ORDERS, ORDER_LINE, and PART tables using
the WHERE clause to specify the relationships between these tables.
8. Repeat Exercise 7, but this time order the rows by item class and
then by order number.
CODE FOR FOLLOWING

SELECT O.ORDER_NUM, O.ORDER_DATE, P.PART_NUM,


P.DESCRIPTION,P.CLASS
FROM ORDERS O, ORDER_LINE OL, PART P
WHERE O.ORDER_NUM=OL.ORDER_NUM
AND OL.PART_NUM=P.PART_NUM
ORDER BY P.CLASS, O.ORDER_NUM;

EXPLANATION 8 :
The query retrieves specific columns from three tables
(ORDERS, ORDER_LINE, and PART) where certain
conditions are met. The result set is then ordered
based on the CLASS and ORDER_NUM columns
9. Use a subquery to find the rep number, last name, and first name of
each sales rep who represents at least one customer with a credit
limit of $10,000. List each sales rep only once in the results.
CODE FOR FOLLOWING

SELECT REP_NUM, LAST_NAME, FIRST_NAME


FROM REP
WHERE REP_NUM IN (SELECT REP_NUM FROM CUSTOMER
WHERE CREDIT_LIMIT >= 10000);

EXPLANATION 9 :

The given SQL query selects the representative number (REP_NUM), last name (LAST_NAME), and
first name (FIRST_NAME) from the REP table for representatives whose REP_NUM is present in the
result of a subquery. The subquery itself selects REP_NUM from the CUSTOMER table for customers
with a CREDIT_LIMIT greater than or equal to 10000
10. Repeat Exercise 9, but this time do not use a subquery

CODE FOR FOLLOWING


SELECT DISTINCT R.REP_NUM, R.LAST_NAME, R.FIRST_NAME,
C.REP_NUM
FROM REP R, CUSTOMER C
WHERE R.REP_NUM=C.REP_NUM
AND C.CREDIT_LIMIT>=10000;

EXPLANTION 10 :
The query retrieves distinct combinations of representative
number, last name, and first name from the REP table, along
with the representative number from the CUSTOMER table,
where the credit limit of the customer is greater than or equal
to 10000.

You might also like