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

CH 12 Simple SQL Queries Assignments

Uploaded by

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

CH 12 Simple SQL Queries Assignments

Uploaded by

Charu Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Create a Table: Create a table called "Employees" with the following


columns:
 emp_id (Employee ID) - INTEGER
 emp_name (Employee Name) - VARCHAR(50)
 emp_age (Employee Age) - INTEGER
 emp_salary (Employee Salary) - DECIMAL(10, 2)
2. Insert Data: Insert at least 5 records into the "Employees" table with
relevant information.
3. Select Data: Write a query to retrieve all records from the
"Employees" table.
4. Select with Condition: Write a query to retrieve employees whose
age is greater than 30.
5. Update Data: Increase the salary of all employees by 10% (update
the emp_salary column).
6. Delete Data: Delete all records of employees whose age is less than
25.
7. Aggregate Functions: Write a query to find the average salary of all
employees.
8. Sorting Data: Write a query to retrieve employees' names and ages
in ascending order of age.
9. Counting Records: Write a query to count the number of employees
in the table.
10. Joining Tables (if applicable): If you have another table called
"Departments" with columns "dept_id" and "dept_name," write a
query to retrieve employees' names and their corresponding
department names using a join operation.

Assignment 1: Create a table called "Books" with the following columns:

 book_id (INT)
 title (VARCHAR)
 author (VARCHAR)
 price (DECIMAL)

Assignment 2: Insert at least 5 rows of data into the "Books" table.

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.

Assignment 7: Create a new table called "Customers" with the following


columns:

 customer_id (INT)
 name (VARCHAR)
 city (VARCHAR)

Assignment 8: Insert at least 3 rows of data into the "Customers" table.

Assignment 9: Write a query to retrieve all the customers from the


"Customers" table.

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".

Assignment 12: Write a query to delete the customer with customer_id = 3


from the "Customers" table.

Which SQL command is used to create a new table in a database? A) ALTER


B) INSERT C) UPDATE D) CREATE

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 5: What is the purpose of the SQL ORDER BY clause? A) To filter


data based on a condition. B) To group rows based on a column. C) To join
two or more tables. D) To sort rows based on a column.

Question 6: Which SQL function is used to calculate the average of a numeric


column? A) AVG B) COUNT C) SUM D) MAX
Question 7: Which SQL command is used to delete data from a table? A)
DELETE B) DROP C) REMOVE D) ERASE

Question 8: What is the purpose of the SQL GROUP BY clause? A) To filter


data based on a condition. B) To sort rows based on a column. C) To group
rows based on a column for aggregate functions. D) To join two or more
tables.

Question 9: Which SQL function is used to count the number of rows in a


table? A) SUM B) AVG C) COUNT D) MAX

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.

Question 1: Consider a table called "Employees" with columns "EmployeeID"


(INT), "Name" (VARCHAR), and "Salary" (DECIMAL). Which SQL query will
retrieve the names of all employees with a salary greater than 5000?

A) SELECT Name FROM Employees WHERE Salary > 5000; B) SELECT


EmployeeID FROM Employees WHERE Salary > 5000; C) SELECT * FROM
Employees WHERE Salary > 5000; D) SELECT Salary FROM Employees
WHERE Salary > 5000;

Question 2: A table called "Orders" has columns "OrderID" (INT),


"CustomerID" (INT), and "OrderDate" (DATE). Which SQL query will retrieve
the total number of orders placed by each customer?

A) SELECT COUNT() FROM Orders; B) SELECT CustomerID, COUNT() FROM


Orders GROUP BY CustomerID; C) SELECT OrderID, CustomerID FROM Orders
GROUP BY CustomerID; D) SELECT COUNT(CustomerID) FROM Orders GROUP
BY CustomerID;

Question 3: Consider two tables: "Students" with columns "StudentID" (INT)


and "Name" (VARCHAR), and "Grades" with columns "StudentID" (INT) and
"Grade" (VARCHAR). Which SQL query will retrieve the names of all students
who scored an "A" grade?

A) SELECT Name FROM Students JOIN Grades ON Students.StudentID =


Grades.StudentID WHERE Grade = 'A'; B) SELECT Name FROM Students
WHERE StudentID IN (SELECT StudentID FROM Grades WHERE Grade = 'A');
C) SELECT Name FROM Students WHERE Grade = 'A'; D) SELECT Name FROM
Students JOIN Grades ON Students.StudentID = Grades.StudentID AND Grade
= 'A';
Question 4: Consider a table called "Books" with columns "BookID" (INT),
"Title" (VARCHAR), and "Author" (VARCHAR). Which SQL query will retrieve
the distinct authors from the "Books" table?

A) SELECT DISTINCT Author FROM Books; B) SELECT UNIQUE Author FROM


Books; C) SELECT Author FROM Books GROUP BY Author; D) SELECT Author
FROM Books;

Question 5: A table called "Sales" has columns "ProductID" (INT), "Quantity"


(INT), and "Price" (DECIMAL). Which SQL query will calculate the total sales
revenue for each product (quantity * price)?

A) SELECT SUM(Quantity * Price) AS Revenue FROM Sales GROUP BY


ProductID; B) SELECT SUM(Quantity * Price) FROM Sales; C) SELECT Revenue
FROM Sales GROUP BY ProductID; D) SELECT ProductID, Quantity * Price AS
Revenue FROM Sales;

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 |

Query 1: Retrieve all students ordered by their marks in descending order.

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.

Table Name: Orders


+---------+------------+------------+
| OrderID | CustomerID | OrderDate |
+---------+------------+------------+
| 1 | 101 | 2023-07-10 |
| 2 | 102 | 2023-07-12 |
| 3 | 101 | 2023-07-15 |
| 4 | 103 | 2023-07-18 |
+---------+------------+------------+

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

Table Name: Products


+----------+----------------+-------+
| ProductID | ProductName | Price |
+-----------+----------------+-------+
| 1 | Laptop | 800 |
| 2 | Smartphone | 600 |
| 3 | Tablet | 400 |
| 4 | Headphones | 100 |
+-----------+----------------+-------+

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;

2. Table Name: Orders

+---------+------------+------------+

| OrderID | CustomerID | OrderDate |


+---------+------------+------------+
| 1 | 101 | 2023-07-10 |
| 2 | 102 | 2023-07-12 |
| 3 | 101 | 2023-07-15 |
| 4 | 103 | 2023-07-18 |
+---------+------------+------------+

Query 3: Calculate the total number of orders in the "Orders" table.

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;

You might also like