โ
Advanced SQL Queries ๐๏ธ๐ก
1๏ธโฃ GROUP BY & HAVING
โฆ GROUP BY groups rows sharing a value to perform aggregate calculations.
โฆ HAVING filters groups based on conditions (like WHERE but for groups).
Example:
Find total sales per product with sales > 1000:
SELECT product_id, SUM(sales) AS total_sales
FROM sales_data
GROUP BY product_id
HAVING SUM(sales) > 1000;
2๏ธโฃ Subqueries
โฆ A query inside another query. Useful for filtering or calculating values dynamically.
Example:
Get customers who placed orders over 500:
SELECT customer_id, order_id, amount
FROM orders
WHERE amount > (SELECT AVG(amount) FROM orders);
3๏ธโฃ Aggregate Functions
โฆ Perform calculations on sets of rows:
โฆ COUNT() counts rows
โฆ SUM() adds numeric values
โฆ AVG() calculates average
โฆ MAX() and MIN() find extremes
Example:
Find average order amount per customer:
SELECT customer_id, AVG(amount) AS avg_order
FROM orders
GROUP BY customer_id;
4๏ธโฃ Complex Joins with Filtering
โฆ Join tables and filter results in one query.
Example:
List customers with orders over100:
SELECT c.customer_name, o.order_id, o.amount
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
WHERE o.amount > 100;
๐ SQL Roadmap: https://whatsapp.com/channel/0029VanC5rODzgT6TiTGoa1v/1506
๐ฌ Double Tap โค๏ธ For More!
Post #2188
5.79K
- โค 7
- ๐ 1