Pragmatic Works Nerd News

SQL Set Operators

Written by Austin Libal | Dec 12, 2023

Introduction

In this blog post, we're diving into the world of SQL set operators with the expert guidance of Austin Libal. Get ready to streamline your data filtering and combining techniques using set operators like intersect and accept. Austin is here to make your SQL journey smoother and more efficient.


Understanding SQL Set Operators

Set operators in SQL, such as intersect, accept, and union, are powerful tools to combine results from different select statements. Austin emphasizes the ease and performance benefits of using set operators over traditional methods like subqueries.


EXCEPT Operator in Action:

  • Scenario: Austin uses the Adventure Works database to demonstrate the EXCEPT operator.
  • Objective: Identify customers who haven't made a purchase by comparing the sales.customer table with the sales.salesorder header table.
  • Result: 701 customers found in the database who have never made a purchase. The accept operator effortlessly filters out customers who haven't appeared in the salesorder header table.

 

Intersect Operator Unveiled:

  • Scenario: Continuing with Adventure Works, Austin now employs the intersect operator.
  • Objective: Find customers who have made a purchase by intersecting the sales.customer and sales.salesorder header tables.
  • Result: 19,119 records reveal customers who have made a purchase, demonstrating the intersect operator's simplicity and efficiency.


Practical Example: Analyzing Product Sales:

  • Use Case: Austin explores another practical example using the intersect operator to analyze product sales.
  • Tables Involved: Production.product and sales.salesorder detail.
  • Objective: Identify products that have been sold by intersecting the two tables.
  • Result: 266 products found, showcasing the power of intersecting records from different tables.

 

Dealing with Unsold Products Using the Accept Operator:

  • Objective: Determine products that haven't been sold by using the accept operator.
  • Result: Austin discovers 266 products that haven't been sold, completing the full picture of product sales.

 

Tips and Takeaways:

  • Austin emphasizes the simplicity of set operators, making them a go-to option for efficient data filtering.
  • He highlights that set operators offer better performance compared to subqueries, which can impact database performance negatively.


Conclusion

And there you have it, a quick guide to SQL set operators by Austin Libal. Whether you're delving into customer data or analyzing product sales, set operators like intersect and accept can make your SQL queries more intuitive and high performing. Don't forget to check out the Pragmatic Works' on-demand learning platform for more insightful content and training sessions on Azure and other Microsoft applications. Like and subscribe to stay updated on the latest SQL tips and tricks.