Category: SQL

It groups together posts about SQL

  • Steps to becoming a SQL ninja: mastering COUNT, SUM, subqueries, JOIN and more

    Are you ready to level up your SQL skills and become a true ninja in database management? Mastering essential SQL functions like COUNT and SUM, as well as advanced techniques like subqueries and joins, is key to unlocking the full potential of your database.

    Here are some steps to help you on your journey to SQL mastery:

    Understanding COUNT and SUM

    Learn how to use COUNT to determine the number of rows in a table or to count specific occurrences of data. Master the art of SUM to calculate the total value of a numeric column or to aggregate data across multiple rows.

    Example 1:

    SELECT COUNT(*) FROM orders;
    

    Example 2:

    SELECT SUM(total_amount) FROM sales;
    

    For more details, give a look here.

    Harnessing the Power of Subqueries

    Explore the versatility of subqueries to perform complex queries within queries. Use them to filter, sort, and manipulate data dinamically:

    Example: SELECT * FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE department_name = ‘Finance’);

    Unleashing the Magic of Joins

    Mastering different types of joins like INNER JOIN, LEFT JOIN and FULL JOIN will put you in a higher position among your colleagues, as it enable you to do more complete and effective queries, providing meaningful insights.

    Source: https://learnsql.com/blog/learn-and-practice-sql-joins

    Example:

    SELECT orders.order_id, customers.customer_name
    FROM orders
    INNER JOIN customers ON orders.customer_id = customers.customer_id;
    

    By mastering these fundamental SQL concepts and techniques, you’ll be well on your way to becoming a ninja in database management. Remember to practice regularly, explore real-world examples, and never stop learning.

    Happy coding!
    — Igor Magalhães Oliveira

  • Mastering SQL joins: A comprehensive guide to Left, Inner, and other Join types

    SQL joins are indispensable tools for extracting meaningful insights from relational databases. Let’s delve into the nuances of left, inner, and other join types with concrete examples to enhance your SQL proficiency.

    However, first of all, let me introduce you to an image i like a lot. Look out and wonder a little bit about it, before going ahead in your reading. It depicts a pretty good Venn Diagram about JOINs:

    Source: https://commons.wikimedia.org/wiki/File:SQL_Joins.svg

    Left Join: A left join retrieves all records from the left table and matching records from the right table based on a common key. It includes unmatched records from the left table and NULL values for unmatched records from the right table.

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

    In this example, the left join retrieves employee names along with their corresponding department names. It ensures that all employee records are included, even if there are no corresponding department records.

    Inner Join: An inner join selects records from both tables where there is a match based on the join condition. It excludes unmatched records from either table.

    SELECT orders.order_id, customers.customer_name
    FROM orders o
    INNER JOIN customers c ON o.customer_id = c.customer_id;
    

    In this example, the inner join retrieves order IDs along with the corresponding customer names. It only includes orders with matching customer records.

    Other Join Types: SQL supports additional join types, including right join, full outer join, and cross join.

    • Right Join: Similar to a left join but prioritizes all records from the right table.
    • Full Outer Join: Combines records from both tables, including unmatched records from either table.
    • Cross Join: Produces the Cartesian product of the two tables, resulting in every possible combination of rows.

    Conclusion: Mastering SQL joins is essential for effective data manipulation and analysis. By understanding left, inner, and other join types, you can optimize your queries and extract valuable insights from your database.

    Experiment with different join scenarios, analyze query performance, and refine your SQL skills to become a proficient database developer. With a solid understanding of SQL joins, you’ll be equipped to tackle complex data challenges and drive informed decision-making.

    Continue exploring SQL join techniques, stay curious, and unlock the full potential of relational databases. Happy querying!

    This comprehensive guide to SQL joins provides concrete examples and insights into left, inner, and other join types, empowering you to optimize your SQL queries and leverage the full capabilities of relational databases.

    Happy coding!
    — Igor Magalhães Oliveira

  • My beloved SQL: practical, illustrated, introductory examples

    I know that you had one of the experiences:

    1. Dealing with a troublesome third-party system where the easier “solution” to some issue is hammering the database;
    2. Producing some advanced report;
    3. Trying to find clues on data.

    If you don’t know what i’m talking about, maybe you have to worry yourself. I explain: it means that you never faced a challenging professional experience, or you’re beginning in the incredible world of IT. Anyway, here is not the place to waste time with it, let’s go ahead!

    See the picture below, where we show an Entity-Relationship Diagram featured in DBeaver software (i strongly recommend using this universal database management tool – it supports various DataBase Management Systems):

    Source: https://dbeaver.com/2022/06/30/two-ways-to-use-erd-in-dbeaver/

    Using this model, imagine we need to show all the rows of ACTOR table where the first_name field has the string “OLIV”. Easy task:

    SELECT * FROM ACTOR WHERE UPPER(FIRST_NAME) LIKE '%OLIV%'
    

    Quite good! We used the operator LIKE along with the brilliant strategy of uppercasing first_name before comparison.

    Now you need to show the name of the actors and the names of the movies they acted. Too easy:

    SELECT a.first_name AS ACTOR_NAME, f.title AS MOVIE_NAME
    FROM actor a, film f, film_actor fa
    WHERE f.film_id = fa.film_id AND a.actor_id = fa_actor_id
    ORDER BY a.first_name;
    

    You can do the same using INNER JOIN:

    SELECT a.first_name AS ACTOR_NAME, f.title AS MOVIE_NAME
    FROM film_actor fa
    INNER JOIN film f ON f.film_id = fa.film_id
    INNER JOIN actor a ON a.actor_id = fa_actor_id
    ORDER BY a.first_name;
    

    For now, it’s enough. Soon we’ll discuss about JOIN and subqueries (Click here). See you there, buds!

    By Igor Magalhães Oliveira