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

CS Elective 4

The document provides examples of SQL queries to perform common operations like inserting, selecting, updating, deleting records from database tables. Some examples include queries to insert a new record, select all records from a table, update a column value, calculate averages and sums, perform inner and outer joins between tables.
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)
9 views5 pages

CS Elective 4

The document provides examples of SQL queries to perform common operations like inserting, selecting, updating, deleting records from database tables. Some examples include queries to insert a new record, select all records from a table, update a column value, calculate averages and sums, perform inner and outer joins between tables.
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/ 5

CS Elective 4

Reviewer
SQL
• Write a SQL query to insert a new record into a table named students with
columns student_id, firstname, middlename, and lastname.
➢INSERT INTO students (student_id, firstname, middlename, lastname)
➢VALUES (1, 'John', 'Doe', 'Smith');
• Retrieve all columns for all records in a table named students.
➢SELECT * FROM students;
• Write a query to select records from a table named products where the
price is greater than 50.
➢SELECT * FROM products WHERE price > 50;
• Write a query to retrieve records from a table named orders and sort them
in descending order based on the order_date column.
➢SELECT * FROM orders ORDER BY order_date DESC;
• Provide a SQL statement to update the quantity column to 100 for the
product with product_id 101.
➢UPDATE products SET quantity = 100 WHERE product_id = 101;
SQL
• Write a query to increase the salary by 10% for all employees in the employees table
whose department is Sales.
➢ UPDATE employees SET salary = salary * 1.1 WHERE department = 'Sales';
• Write a query to delete all records from a table named logs without deleting the table
structure.
➢ DELETE FROM logs;
• Write a query to delete all records from a table named products where the
stock_quantity is less than or equal to 10.
➢ DELETE FROM products WHERE stock_quantity <= 10;
• Write a query to find the total number of records in a table named students.
➢ SELECT COUNT(*) FROM students;
• Write a query to calculate the average value of the price column in a table named
products.
➢ SELECT AVG(price) FROM products;
• Write a query to find the total sales (revenue) for each category in the products table.
➢ SELECT category, SUM(price * quantity) AS total_sales
➢ FROM products
➢ GROUP BY category;
SQL
• Write a query to determine the number of unique products in the inventory table.
➢ SELECT COUNT(DISTINCT product_id) FROM inventory;
• Write a query to retrieve all the employee_name from employees table and matchining
department_name from the departments tables, assuming that an employee belongs to
a single department
➢ SELECT employees.employee_name, departments.department_name
➢ FROM employees
➢ JOIN departments ON employees.department_id = departments.department_id;
• Write a query to join between the departments and students tables, retrieving all
records from the students table and matching department_name from the department
table.
➢ SELECT students.*, departments.department_name
➢ FROM students
➢ JOIN departments ON students.department_id = departments.department_id;
• Write a SQL query to display all records from the customers table and the corresponding
orders (if any) from the orders table using a left join.
➢ SELECT *
➢ FROM customers
➢ LEFT JOIN orders ON customers.customer_id = orders.customer_id;

You might also like