Advanced Projects | Dataplexa

Advanced SQL Projects

You have now reached the Advanced SQL stage.

In this lesson, we work through real-world advanced SQL projects that combine performance, analytics, and database engineering concepts.

These projects reflect how SQL is used in professional environments.


Project 1: Employee Performance Analytics

Scenario: A company wants to analyze employee salaries within each department.

Requirements:

  • Rank employees by salary within departments
  • Show department average salary
  • Preserve individual employee records

Solution

SELECT name,
       department,
       salary,
       RANK() OVER (
         PARTITION BY department
         ORDER BY salary DESC
       ) AS salary_rank,
       AVG(salary) OVER (
         PARTITION BY department
       ) AS dept_avg_salary
FROM employees;
  

This project uses window functions to perform analytics without grouping rows.


Project 2: Organizational Hierarchy

Scenario: Generate a full organizational chart from a manager–employee relationship.

Requirements:

  • Show all levels of hierarchy
  • Include reporting depth

Solution

WITH RECURSIVE org_chart AS (
  SELECT id, name, manager_id, 1 AS level
  FROM employees
  WHERE manager_id IS NULL

  UNION ALL

  SELECT e.id, e.name, e.manager_id, oc.level + 1
  FROM employees e
  JOIN org_chart oc
  ON e.manager_id = oc.id
)
SELECT * FROM org_chart;
  

Recursive queries allow unlimited hierarchy depth.


Project 3: JSON-Based User Profiles

Scenario: Store and query flexible user profile data.

Requirements:

  • Store preferences as JSON
  • Query users by JSON attributes

Solution

SELECT id,
       profile->>'$.city' AS city,
       profile->>'$.role' AS role
FROM users
WHERE profile->>'$.role' = 'Admin';
  

This approach supports evolving schemas.


Project 4: Dynamic Reporting Query

Scenario: Build a report where the filter column changes dynamically.

Requirements:

  • Dynamic column selection
  • Safe execution

Solution

SET @column = 'department_id';
SET @value = 1;

SET @sql = CONCAT(
  'SELECT name, salary FROM employees WHERE ',
  @column, ' = ', @value
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
  

Dynamic SQL enables flexible reporting systems.


Project 5: Performance Optimization

Scenario: Improve slow department-based queries.

Steps:

  • Analyze query execution plan
  • Add appropriate index

Solution

EXPLAIN
SELECT name, salary
FROM employees
WHERE department_id = 2;

CREATE INDEX idx_dept
ON employees(department_id);
  

Indexes dramatically reduce execution time.


Concepts Applied in This Lesson

  • Window functions
  • Recursive queries
  • JSON data handling
  • Dynamic SQL
  • Index tuning

Why These Projects Matter

These projects simulate:

  • Enterprise analytics
  • HR and reporting systems
  • Flexible application backends
  • Performance-critical environments

Mastering these patterns prepares you for real SQL engineering roles.


What’s Next?

In the final lesson, we will complete a Mini SQL Project that combines everything learned from beginner to advanced levels.