IKH

Set Operations with SQL

Can you recall the types of set operations that you came across at the beginning of this session? They were union, intersection and difference. In MySQL, these operations are achieved using the keywords ‘union’ and ‘union all’. Unfortunately, MySQL Workbench does not support ‘intersect’ and ‘minus’ keywords; these operations are performed with a combination of other SQL clauses that you are familiar with by now. You may find the two links provided below helpful for reading more about these two operations.

From the next video, you will get an understanding of the difference between using the ‘union’ and ‘union all’ operators and learn how they are implemented in queries.

As Professor Ramanathan explained in the video given above, always make sure that the tables or the results of your queries are union-compatible before you perform a union operation on them. Two tables are union-compatible if:

  • They have the same number of attributes, and
  • The attribute types are compatible, i.e., the corresponding attributes have the same data type.

Additional resources

  • MySQL INTERSECT is a useful┬álink to help you understand how you can emulate intersect operations in MySQL.
  • Similarly, you can implement minus operations in MySQL. You can go to┬áthis MySQL MINUS to know more about it.

In the next segment, you will have some practice questions to have more hands-on using SQL queries.

Report an error