CH 12 Simple SQL Queries Assignments
CH 12 Simple SQL Queries Assignments
book_id (INT)
title (VARCHAR)
author (VARCHAR)
price (DECIMAL)
Assignment 3: Write a query to retrieve all the books from the "Books" table.
Assignment 4: Write a query to retrieve the books where the price is greater
than 50.
Assignment 5: Write a query to update the price of the book with book_id = 3
to 65.50.
Assignment 6: Write a query to delete the book with book_id = 2 from the
"Books" table.
customer_id (INT)
name (VARCHAR)
city (VARCHAR)
Assignment 10: Write a query to retrieve the customers who are from the
city "New York".
Assignment 11: Write a query to update the name of the customer with
customer_id = 2 to "Jane Smith".
Question 2: What does the SQL SELECT statement do? A) Insert new data
into a table. B) Retrieve data from a table. C) Delete data from a table. D)
Update data in a table.
Question 3: Which SQL keyword is used to retrieve specific rows from a table
based on a condition? A) SELECT B) WHERE C) FROM D) ORDER BY
Question 4: Which SQL command is used to insert new data into a table? A)
ADD B) INSERT C) CREATE D) UPDATE
Question 10: What does the SQL WHERE clause do? A) It sorts the rows of the
table. B) It groups rows based on a column. C) It retrieves specific rows
based on a condition. D) It calculates the average of a column.
Answers:
1. A
2. B
3. A
4. A
5. A
Table Name: Students
+------+---------------+---------+-------+
| ID | Name | Grade | Marks |
+------+---------------+---------+-------+
| 1 | John Doe | A | 85 |
| 2 | Jane Smith | B | 72 |
| 3 | Michael Brown | A | 90 |
| 4 | Emily Johnson | C | 65 |
sqlCopy code
SELECT * FROM Students ORDER BY Marks DESC ;
Query 2: Retrieve the names of students who scored an "A" grade, ordered
alphabetically by name.
Query 3: Retrieve all orders grouped by the CustomerID, showing the count
of orders placed by each customer.
sqlCopy code
SELECT CustomerID, COUNT ( * ) AS OrderCount FROM Orders GROUP BY CustomerID;
Query 4: Retrieve all orders grouped by the year of the OrderDate, showing
the count of orders placed in each year.
sqlCopy code
SELECT YEAR (OrderDate) AS OrderYear, COUNT ( * ) AS OrderCount FROM Order
Query 6: Retrieve the product names and the average price of each product,
ordered alphabetically by product name.
sqlCopy code
SELECT ProductName, AVG (Price) AS AvgPrice FROM Products GROUP BY ProductName
ORDER BY ProductName;
+---------+------------+------------+
sqlCopy code
SELECT COUNT ( * ) AS TotalOrders FROM Orders;
Query 4: Calculate the latest order date from the "Orders" table.
sqlCopy code
SELECT MAX (OrderDate) AS LatestOrderDate FROM Orders;