Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add advanced SQL queries #246

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions sql-queries/sql-queries-advanced/sql-queries-advanced.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- Query with subquery and join
SELECT *
FROM employees e
WHERE salary > (
SELECT AVG(salary)
FROM employees
);

-- Query with window functions
WITH ranked_employees AS (
SELECT *,
ROW_NUMBER() OVER (ORDER BY salary DESC) AS rank
FROM employees
)
SELECT *
FROM ranked_employees
WHERE rank <= 10;

-- Query with CTE (Common Table Expression)
WITH sales_summary AS (
SELECT department_id, SUM(amount) AS total_sales
FROM sales
GROUP BY department_id
),
average_sales AS (
SELECT AVG(total_sales) AS avg_sales
FROM sales_summary
)
SELECT s.department_id, s.total_sales, a.avg_sales
FROM sales_summary s
JOIN average_sales a ON s.department_id = a.department_id;