SQL practice questions

 1. Write a SQL query to insert new record in employes table?

INSERT INTO employees (employee_id, first_name, last_name, email, phone_number, hire_date, job_id, salary, department_id)
VALUES (101, 'John', 'Doe', 'john.doe@example.com', '123-456-7890', '2024-10-01', 'IT_PROG', 60000, 10);

2. Write a query to update the quantity column in products to 10?

UPDATE products SET quantity = 50 WHERE product_id = 101;

3. Write a SQL query to delete all the records from the orders table where the status is cancelled?

DELETE FROM orders WHERE status = 'cancelled';

4. Write a SQL query to retreive all the columns from the product table where the category is electronics and the price is less than 1000?

SELECT * FROM product WHERE category = 'electronics' AND price < 1000;

5.  Write a SQL query to retreive names of all the customers in alphabetic oder?

SELECT customer_name FROM customers ORDER BY customer_name ASC;

6. Write a SQL query to retreive the total no of orders from orders table?

SELECT COUNT(*) AS total_orders FROM orders;

7. Write a SQL query to retrieve the product name, category, and supplier name from the "products", "categories", and "suppliers" tables, joining them on the appropriate columns.?

SELECT p.product_name, c.category_name, s.supplier_name 
FROM products p
JOIN categories c ON p.category_id = c.category_id
JOIN suppliers s ON p.supplier_id = s.supplier_id;

8. Write a SQL query to retrieve the employee name and department name from the "employees" and "departments" tables, joining them on the "department_id" column.?

SELECT e.employee_name, d.department_name FROM employees e JOIN departments d ON e.department_id = d.department_id;

9. Write a SQL query to retrieve the customer name and order amount from the "customers" and "orders" tables, joining them on the "customer_id" column, and only including orders with amounts greater than 1000.?

SELECT c.customer_name, o.order_amount FROM customers c JOIN orders o ON c.customer_id = o.customer_id WHERE o.order_amount > 1000;

10. Write a SQL query to retrieve the average price of products in each category from the "products" table.?

SELECT category_id, AVG(price) AS average_price FROM products GROUP BY category_id;

11. Write a SQL query to retrieve the maximum salary for each department from the "employees" table.?

SELECT department_id, MAX(salary) AS max_salary FROM employees GROUP BY department_id;

12. Write a SQL query to retrieve the total revenue generated by each customer from the "orders" and "order_items" tables.

SELECT o.customer_id, SUM(oi.quantity * oi.price) AS total_revenue FROM orders o JOIN order_items oi ON o.order_id = oi.order_id GROUP BY o.customer_id;

13. Write a SQL query to delete all records from the "customers" table where the "last_login_date" is older than 1 year?

DELETE FROM customers WHERE last_login_date < NOW() - INTERVAL 1 YEAR;

14. Write a SQL query to update the "discount" column of the "orders" table by increasing it by 5% for all orders placed before a specific date.

UPDATE orders SET discount = discount * 1.05 WHERE order_date < '2023-01-01';

15. Write a SQL query to retrieve all the products with a price either above 1000 or below 500.

SELECT * FROM products WHERE price > 1000 OR price < 500;

16. Write a SQL query to retrieve the employees who were hired between a specific date range?

SELECT * FROM employees WHERE hire_date BETWEEN '2023-01-01' AND '2023-12-31';

17. Write a SQL query to retrieve all the customers who do not have a phone number specified in the database.?

SELECT * FROM customers WHERE phone_number IS NULL OR phone_number = '';

18. Write a SQL query to retrieve the current date and time.?

SELECT NOW() AS current_datetime;

19. Write a SQL query to retrieve the uppercase names of all the employees from the "employees" table.?

SELECT UPPER(first_name) AS first_name, UPPER(last_name) AS last_name FROM employees;

20. Write a SQL query to retrieve the average price of products after applying a 10% discount from the "products" table.?

SELECT AVG(price * 0.9) AS average_discounted_price FROM products;

21. Create a view named "order_summary" that retrieves the total order amount and the number of orders for each customer from the "orders" table.

CREATE VIEW order_summary AS SELECT customer_id, SUM(order_amount) AS total_order_amount, COUNT(order_id) AS number_of_orders FROM orders GROUP BY customer_id;

22. Create an index on the "email" column of the "customers" table for faster searching.?

CREATE INDEX idx_email ON customers(email);

23. Create a view named "product_inventory" that retrieves the product name and the available quantity for each product from the "products" and "inventory" tables.

CREATE VIEW product_inventory AS SELECT p.product_name, i.available_quantity FROM products p JOIN inventory i ON p.product_id = i.product_id;

24. Write a SQL query to start a transaction, delete all records from the "orders" table, and roll back the transaction.?

START TRANSACTION; DELETE FROM orders; ROLLBACK;

25. Write a SQL query to update the "balance" column of the "accounts" table by adding a specific amount for a specific account, ensuring the consistency of the transaction.?

START TRANSACTION; UPDATE accounts SET balance = balance + 100 WHERE account_id = 'SPECIFIC_ACCOUNT_ID'; COMMIT;

26. Write a SQL query to lock a specific row in the "employees" table to prevent other transactions from modifying it.?

SELECT * FROM employees WHERE employee_id = 'SPECIFIC_EMPLOYEE_ID' FOR UPDATE;

HCF and LCM

 If Remainder same:

HCF(n1-R, n2-R, n3-R)

LCM(n1,n2,n3) - R

If Remainder is Different:

HCF(n1-r1, n2-r2, n3-r3)

LCM(n1, n2, n3)-(n1-r1) if n1-r1 = n2-r2 = n3-r3

Surface area and Volumes

 Surface area and Volumes

Cube

Lateral surface area: 4a**2

Total surface area: 6a**2

Volume: a**3

Cuboid

Lateral surface area: 2(l+b)h

Total surface area: 2(lb+bh+hl)

Volume: l*b*h

Cylinder

Curved Surface area: 2*pi*r*h

Total surface area: 2*pi*r(r+h)

Volume: pi*r**2*h

Cone:

Lateral surface area: pi*r*l

Total surface area: pi*r(r+l)

Volume: 1/3*pi*r**2*h

Sphere:

Surface area: 4*pi*r**2*h

Volume: 4/3*pi*r**2*h

Simple Interest

 1. Difference between simple interest & compound interest for 2 years = pr**2/100**2

2. Same for 3 years = pr**2(300+r)/100**3

3. Compound Interest > Simple Interest

4. Simple Interest = PTR/100

5. Amount = Principle + interest

6. Compound Interest = P(1+r/100)**n - p

Work and Time

 Work and Time Apptitude Questions

Trick 1If M1 men can finish W1 work in D1 days and M2 men can finish W2 work in D2 days then, the relation is: M1*D1/W1 = M2*D2/W2

Trick 2: If M1 men finish W1 work in D1 days, working T1 time each day and M2 men finish W2 work in D2 days, working T2 time each day, then the relation is: M1*D1*T1/W1 = M2*D2*T2/W2

Trick 3: If A completes a piece of work in ‘x’ days, and B completes the same work in ‘y’ days, then, 

Work done by A in 1 day = 1/X

Work done by B in 1 day = 1/Y

Work done by A and B in 1 day = 1/X + 1/Y 

Total time taken to complete the work by A and B both = XY/(X + Y)

Trick 4: If A can do a work in ‘x’ days, B can do the same work in ‘y’ days, C can do the same work in ‘z’ days, then the total time taken by A, B and C to complete the work together = XYZ/(XY + YZ + ZX)

Trick 5:  If A alone can do a certain work in ‘x’ days and A and B together can do the same work in ‘y’ days, then B alone can do the same work in = XY/(X – Y)

Trick 7: If A can do a work in ‘x’ days and B can do the same work in ‘y’ days and when they started working together, B left the work ‘m’ days before completion then the total time taken to complete work is = (Y + M)X / (X+Y)

Trick 8: If A leaves the work ‘m’ days before its completion then the total time taken to complete work is = (X + M)Y / (X+Y)


Popular Post

MindMaps

Featured post

Question 1: Reverse Words in a String III

  def reverseWords(s: str) -> str: words = s.split() return ' '.join(word[::-1] for word in words)