DEV Community

Kelvin Kipyegon
Kelvin Kipyegon

Posted on • Edited on

Mastering SQL for Data Engineering: Advanced Queries, Optimization, and Data Modeling Best Practices

SQL is mainly used by Data Engineers to bring data together and run queries that turn raw data into useful business insights. Data Engineers use SQL to change various aspects of the database eg tables and pull out specific data to be used for different purposes. In this article, we will explore advanced SQL techniques, optimization strategies, and data modeling best practices that will help you handle complex data engineering tasks.


Core SQL Concepts for Data Engineering

SELECT, WHERE, JOIN, GROUP BY, and HAVING

The most basic SQL commands are essential for performing almost any data engineering task.

  • SELECT: Retrieves data from a database.
  SELECT * FROM employees;
Enter fullscreen mode Exit fullscreen mode

This query fetches all the columns and rows from the employees table. The * symbol indicates that all columns should be retrieved.****

  • WHERE: Filters data based on conditions.
  SELECT * FROM employees WHERE department = 'HR';
Enter fullscreen mode Exit fullscreen mode

This query selects only those rows from the employees table where the department is 'HR'. It acts as a filter to get data based on a specific condition.

  • JOIN: Combines data from multiple tables.
  SELECT e.name, d.department_name 
  FROM employees e 
  JOIN departments d ON e.department_id = d.id;
Enter fullscreen mode Exit fullscreen mode

This query combines the employees table and the departments table by joining them on the department_id column. It retrieves employee names along with their department names.

  • GROUP BY: Groups rows based on column values.
  SELECT department, COUNT(*) 
  FROM employees 
  GROUP BY department;
Enter fullscreen mode Exit fullscreen mode

This query groups employees by their department and counts how many employees are in each department.

  • HAVING: Filters groups after applying GROUP BY.
  SELECT department, COUNT(*) 
  FROM employees 
  GROUP BY department 
  HAVING COUNT(*) > 5;
Enter fullscreen mode Exit fullscreen mode

This query counts employees per department but only returns those departments where the count is greater than 5. The HAVING clause filters the result of GROUP BY.


Advanced SQL Techniques

Once you’re comfortable with basic SQL, you can explore more advanced techniques that make SQL even more powerful.

Recursive Queries and Common Table Expressions (CTEs)

Recursive Queries and Common Table Expressions (CTEs)


Introduction to Recursive Queries and Common Table Expressions (CTEs)

When working with SQL, sometimes you need to deal with data that has a hierarchy or structure like a family tree or an organization chart. Recursive Queries and Common Table Expressions (CTEs) are helpful tools to manage this type of data.

  • CTE (Common Table Expression): Think of a CTE as a temporary table that you create in the middle of your query. It simplifies complex queries and makes them easier to read and maintain.

  • Recursive Queries: These are a special kind of CTE that allows you to reference the same table or CTE multiple times to build hierarchical data, such as parent-child relationships (e.g., employees and their managers).


How CTEs Work

Let’s start with a simple CTE. Here's an example of how to use a CTE to get a list of employees and their departments:

WITH EmployeeDetails AS (
  SELECT name, department
  FROM employees
)
SELECT * FROM EmployeeDetails;
Enter fullscreen mode Exit fullscreen mode
  • The WITH keyword defines the CTE called EmployeeDetails.
  • Inside the CTE, we select the name and department from the employees table.
  • The second SELECT retrieves data from the CTE we just created. It’s like working with a temporary table called EmployeeDetails.

Recursive Query Example

Now, let’s look at a recursive query, which is useful when working with data that has parent-child relationships, like managers and their employees.

Here’s an example where we find all employees under a specific manager, even if those employees have their own subordinates:

WITH RECURSIVE EmployeeHierarchy AS (
  -- Base case: Select the manager (top-level employee)
  SELECT id, manager_id, name
  FROM employees
  WHERE manager_id IS NULL

  UNION ALL

  -- Recursive case: Select employees under the current employees
  SELECT e.id, e.manager_id, e.name
  FROM employees e
  JOIN EmployeeHierarchy eh ON e.manager_id = eh.id
)
SELECT * FROM EmployeeHierarchy;
Enter fullscreen mode Exit fullscreen mode
  • The CTE EmployeeHierarchy starts by selecting the top-level employees (those without managers) as the base case (manager_id IS NULL).
  • The UNION ALL combines the base case with the recursive part. The second part of the CTE selects employees whose manager_id matches the id of someone already in the hierarchy.
  • This query keeps running until it has found all employees in the hierarchy.

The result is a list of employees along with their managers, regardless of how many levels deep the hierarchy goes.


When to Use Recursive Queries and CTEs

Recursive queries and CTEs are helpful when you need to:

  • Work with hierarchical data (e.g., organizational charts, categories of products).
  • Simplify complex queries that would otherwise require multiple subqueries or joins.
  • Improve the readability and maintenance of SQL queries.

--

Query Optimization and Performance Tuning

Understanding Execution Plans and Query Profiling

To optimize SQL queries, it's essential to understand how the database executes them. Execution plans provide insights into how the query is processed, highlighting areas for improvement.

  • Use the EXPLAIN command to view the execution plan:
  EXPLAIN SELECT * FROM employees WHERE salary > 50000;
Enter fullscreen mode Exit fullscreen mode

Indexing Strategies to Speed Up Query Performance

Indexes help speed up the retrieval of data. Properly indexed columns significantly reduce query times, especially for large datasets.

  • Create an index on frequently queried columns:
  CREATE INDEX idx_employee_department ON employees(department_id);
Enter fullscreen mode Exit fullscreen mode

Techniques for Reducing Query Complexity and Improving Efficiency

  • *Avoid SELECT **: Instead of selecting all columns, only select the ones you need.
  • Limit Joins: Keep joins to a minimum to reduce data complexity.
  • Optimize Subqueries: Subqueries can sometimes be replaced by joins or temporary tables for better performance.

Data Modeling Best Practices

Normalization vs. Denormalization—When to Use Each Approach

  • Normalization organizes data into smaller tables to reduce redundancy and improve data integrity.
  • Denormalization combines tables to make queries faster at the cost of redundancy.

In data engineering, denormalization is often preferred in analytical systems for faster read operations, while normalization is used in transactional systems to ensure data consistency.

Designing Efficient Relational Schemas

When designing a database schema, focus on scalability and performance. Use appropriate primary keys, foreign keys, and indexes to make data retrieval faster and more reliable.

Star Schema vs. Snowflake Schema for Analytical Queries

  • Star Schema: Simple, with a central fact table and dimension tables connected directly. It’s fast for queries but may involve some redundancy.

  • Snowflake Schema: More complex, with dimension tables normalized into additional tables. It reduces redundancy but may require more joins in queries.


Example of Optimizing a Slow SQL Query

Let’s say we have a query that calculates total sales for each product category, but it’s running too slow:

SELECT category, SUM(sales_amount) 
FROM sales 
GROUP BY category;
Enter fullscreen mode Exit fullscreen mode

This query can be optimized by adding an index on the category column:

CREATE INDEX idx_category ON sales(category);
Enter fullscreen mode Exit fullscreen mode

Mastering SQL is essential for data engineers to handle complex data operations and optimize workflows. By understanding advanced SQL techniques, query optimization, and best practices for data modeling, you can improve the efficiency of your data pipelines and make better business decisions. Keep experimenting with different SQL features, and apply these techniques in real-world projects to continue improving your skills.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay