0% found this document useful (0 votes)
9 views28 pages

Ch05 SQL Supp

Uploaded by

Bryan alex
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)
9 views28 pages

Ch05 SQL Supp

Uploaded by

Bryan alex
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/ 28

Which products have a standard price of less than $275?

SELECT ProductID, ProductDescription, ProductStandardPrice


FROM Product-T
WHERE ProductStandardPrice < 275;

Output:
ProductID ProductDescription ProductStandardPrice
1 End Table $175
2 Coffee Table $200
8 Computer Desk $250
Figure-1 - Sample Pine Valley Furniture Company data Slide
3
Slide
4
Slide
6
Slide
7
Slide
9
Slide
17
Slide
18
Slide 23

Slide 24

Slide 25
Which orders have been placed since 10/24/2010?(Refer to Figure-1)

SELECT OrderID, OrderDate


FROM Order_T
WHERE OrderDate > ‘24-Oct-2010’;

Output:

OrderID OrderDate
1007 10/27/2010
1008 10/30/2010
1009 11/05/2010
1010 11/05/2010
Which furniture does Pine Valley carry that isn’t made of cherry?(Refer to
Figure-1)

SELECT ProductDescription, ProductFinish


FROM Product_T
WHERE ProductFinish != ‘Cherry’;

Output:

ProductDescription ProductFinish

Coffee Table Natural Ash


Computer Desk Natural Ash
Entertainment Center Natural Maple
8-Drawer Desk White Ash
Dining Table Natural Ash
Computer Desk Walnut
Who is(are)the customer(s) whose age is 21 or above and from
Australia?

SELECT * FROM Customer


WHERE Age >= 21 and Country = ‘Australia’;

Customer Table

Output:
CustomerID CustomerName LastName Country Age Phone
2 Aman Chopra Australia 21 XXXXXX
Which products in the Product table have a standard price between
$200 and $300? (Refer to Figure-1)

SELECT ProductDescription, ProductStandardPrice


FROM Product_T
WHERE ProductStandardPrice >= 200 and
ProductStandardPrice <= 300;

Output:
ProductDescription ProductStandardPrice
Coffee Table 200
Computer Desk 250
List product name, finish, and standard price for all desks and all tables
that cost more than $300 in the Product table. (Refer to Figure-1)

SELECT ProductDescription, ProductFinish, ProductStandardPrice


FROM Product_T
WHERE ProductDescription LIKE ’%Desk’
OR ProductDescription LIKE ‘%Table’
AND ProductStandardPrice > 300;
Output:

ProductDescription ProductFinish
ProductStandardPrice

Computer Desk Natural Ash


375
Writer’s Desk Cherry 325
8-Drawer Desk White Ash
750
Dining Table Natural Ash 800
List product name, finish, and standard price for all desks and tables in
the product table that cost more than $300. (Refer to Figure-1)

SELECT ProductDescription, ProductFinish, ProductStandardPrice


FROM Product_T
WHERE (ProductDescription LIKE ’%Desk’
OR ProductDescription LIKE ‘%Table’)
AND ProductStandardPrice > 300;
Output:

ProductDescription ProductFinish
ProductStandardPrice

Computer Desk Natural Ash


375
Writer’s Desk Cherry 325
8-Drawer Desk White Ash
750
Dining Table Natural Ash 800
Modify(update) all employee records by increasing the Age by 5 years
and adding $3000 to Salary field.

UPDATE Employee
SET Age = Age + 5, Salary = Salary + 3000;

Employee Table
ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 Kaushik 23 Kota 2500.00

Output:
ID NAME AGE ADDRESS SALARY
1 Ramesh 37 Ahmedabad 5000.00
2 Khilan 30 Delhi 4500.00
3 Kaushik 28 Kota 5500.00
What is the employee name whose salary is smaller than or equal to
$1,500 in the Employee table?

SELECT MIN(Name), Salary


FROM Employee
WHERE Salary <=1500;

Output:

EmployeeName Salary
Khilan 1500
Identify(count) the number of customers whose country is the UK.

SELECT Count(country)
FROM Customer
WHERE country = ‘UK’;

Customer table
CustomerID FirstName LastName Age Country
1 John Joe 31 USA
2 Robert Luna 22 USA
3 David Robinson 22 UK
4 John Reinhardt 25 UK
5 Betty Doe 28 UAE

Output:
COUNT(country)
2
Figure-2 - Sample Pine Valley Furniture Company
data

Order_T Customer_T

Slide 1
5
Slide 2
2
Count the number of customers in each state to which we ship. (Refer to Figure-2)

SELECT CustomerState, COUNT(CustomerState)


FROM Customer_T
GROUP BY CustomerState;

Output:
CustomerState Count(CustomerState)
CA 2
CO 1
FL 3
HI 1
MI 1
NJ 2
NY 1
PA 1
TX 1
UT 1
WA 1
Display for each employee the difference between salary and the overall
salary(average salary).

SELECT Salary, AvgSalary, Salary – AvgSalary AS Difference


FROM Employee, (SELECT AVG(Salary) AS AvgSalary
FROM Employee);

Employee table
ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 Kaushik 23 Kota 2500.00

Output:
Salary AvgSalary Difference
2,000 2,000 0
1,500 2,000 -500
2,500 2,000 500
List all customers who live in warmer states. (Refer to Figure-1)

SELECT CustomerName, CustomerCity, CustomerState


FROM Customer_T
WHERE CustomerState IN (‘FL’, ‘TX’, ‘CA’, ‘HI’);

Output:

CustomerName CustomerCity CustomerState

Contemporary Casuals Gainesville FL


Value Furniture Plano TX
Impressions Sacramento CA
California Classics Santa Clara CA
M and H Casual Furniture Clearwater FL
Seminole Interiors Seminole FL
Kaneohe Homes Kaneohe HI
List customer, city, and state for all customers in the Customer table whose address is Florida,
Texas, California, or Hawaii. List the customers alphabetically by state and alphabetically by
customer within each state. (Refer to Figure-1)

SELECT CustomerName, CustomerCity, CustomerState


FROM Customer_T
WHERE CustomerState IN (‘FL’, ‘TX’, ‘CA’, ‘HI’)
ORDER BY CustomersState, CustomerName;

Output:

CustomerName CustomerCity CustomerState

California Classics Santa Clara CA


Impressions Sacramento CA
Contemporary Casuals Gainesville FL
M and H Casual Furniture Clearwater FL
Seminole Interiors Seminole FL
Kaneohe Homes Kaneohe HI
Value Furniture Plano TX
Student table

Student_ID Student_Name Course Age Marks

101 Anuj Tech 20 88


102 Raman MCA 24 98
104 Shyam BBA 19 92
107 Vikash Tech 20 88
111 Monu MBA 21 65
114 Jones Tech 18 93
121 Parul BCA 20 97
123 Divya Tech 21 98
128 Hemant MBA 23 90
130 Nidhi BBA 20 65
132 Priya MBA 22 99
138 Mohit MCA 21 88
Based on the details of the student table, calculate the
average of those student marks which are greater than 90.

SELECT AVG(Marks) As Average Marks>90


FROM Student
HAVING AVG(Marks)> 90;

Output:
Average Marks>90
96
Based on the details of the student table, calculate the
average student marks by course.

SELECT Course, AVG(Marks)


FROM Student
GROUP BY Course;

Output:
Course AVG(Marks)
BBA 78
BCA 97
MBA 84
MCA 93
Tech 91
List and display CustomerID, CustomerName and
CustomerPostalCode for those customers who live in
California(CA) or Washington(WA), order by
CustomerPostalCode in descending order. (
Refer to Figure-2)

SELECT CustomerID, CustomerName, CustomerPostalCode


FROM Customer_T
WHERE CustomerState IN (‘WA’,’CA’)
ORDER BY PostalCode DESC;
OR
SELECT CustomerID, CustomerName, CustomerPostalCode
FROM Customer_T
WHERE CustomerState = ‘WA’ OR CustomerState = ‘CA’
ORDER BY PostalCode DESC;
For every product that has been ordered, display the product ID and
the total quantity ordered (label this result TotalOrdered). List the
most popular product first and the least popular last.
(Refer to Figure-1)

SELECT ProductID, SUM(OrderedQuantity) AS TotalOrdered


FROM OrderLine_T
GROUP BY ProductID
ORDER BY SUM(OrderedQuantity) DESC;
Display the product ID and the number of orders placed for each product. Show
the results in decreasing order, and label result column NumOrders.
(Refer to Figure-1)

SELECT ProductID, COUNT(ProductID) AS NumOrders


FROM OrderLine_T
GROUP BY ProductID
ORDER BY COUNT(ProductID) DESC;
List and display product ID, product finish, standard price, and thickness for
products made of cherry, pine, or walnut. Order the list by product finish and
product standard price. Refer to Figure -1

SELECT ProductID, ProductFinish, ProductStandardPrice, Thickness


FROM Product_T
WHERE ProductFinish IN (‘Cherry’, ‘Pine’, ‘Walnut’)
ORDER BY ProductFinish, ProductStandardPrice;
OR
SELECT ProductID, ProductFinish, ProductStandardPrice, Thickness
FROM Product_T
WHERE ProductFinish = ‘Cherry’ or ProductFinish = ‘Pine’
or ProductFinish = ‘Walnut’
ORDER BY ProductFinish, ProductStandardPrice;
Identify and display product finish and width for products that are
not cherry or oak and whose width is greater than 10 inches.

SELECT ProductFinish, Width


FROM Product_T
WHERE ProductFinish NOT IN (“Cherry”, “Oak”)
AND Width > 10;
Show ProductID, ProductDescription, ProductFinish, and
ProductStandardPrice for oak products with price > 400 or cherry
products with price < 300.

SELECT ProductID, ProductDescription, ProductFinish,


ProductStandardPrice
FROM Product_T
WHERE (ProductFinish = ‘Oak’ and ProductStandardPrice >
400)
or (ProductFinish = ‘Cherry’ and ProductStandardPrice <
300);
Select and display Customer ID and total orders placed in 2010.

SELECT CustomerID, Count(OrderID) AS TotOrders

FROM Order_T

WHERE OrderDate Between ‘01-Jan-2010’ and ‘31-Dec-2010’

GROUP BY CustomerID;

You might also like