![]() |
MariaDB, a popular open-source relational database management system (RDBMS), offers a plethora of powerful features for data manipulation and querying. Among these features is the Intersect operator, a valuable tool for performing set operations on query results. In this article, We will learn bout the intersect operator in MongoDB along with various examples and so on. Intersect Operator in MariaDBThe Intersect operator is an operator that is used to retrieve the common records from two or more tables. It works by comparing the result sets of two or more queries and returning only the rows that appear in all result sets. It makes the intersect a useful operator for finding common elements between datasets. Syntax: The syntax for using the Intersect operator in MariaDB is straightforward: SELECT col1, col2, ... FROM table1 INTERSECT SELECT col1, col2, ... FROM table2; Explanation:
It’s important to note that the number of columns and their data types in the SELECT queries must match for the Intersect operator to work correctly. The following picture illustrates the intersect operation: ![]() Intersection Examples of Intersect Operator in MariaDBLet’s understand with the use of the Intersect operator with some practical examples. To understand the Intersect Operator in good manner, We need table on which we will perform various operations. Let’s create tables and insert some data into it. Create table employee_a: CREATE TABLE employees_a ( Insert data: INSERT INTO employees_a (employee_id, first_name, last_name, department, salary) VALUES Output: ![]() employee_a Table Create table employee_b: CREATE TABLE employees_b ( Insert data: INSERT INTO employees_b (employee_id, first_name, department,salary) VALUES Output: ![]() employee_b Table Example 1: Return Single Field Using INTERSECT OperatorSuppose we have two tables, employees_a and employees_b, both containing information about customers. We want to find employees who exist in both tables. Query: SELECT first_name FROM employees_a Output: ![]() Single Field Explanation: In the above query, We uses the INTERSECT operator to retrieve the common first_name values from both employees_a and employees_b tables. As we can see in the output it return only the single field first_name Minal which is common in both the tables. Example 2: Intersecting Multiple FieldsWe can also use the Intersect operator to find common records among multiple queries. Query: (SELECT employee_id, first_name FROM employees_a WHERE department = 'Sales') Output: ![]() Multiple Fields Explanation:
Example 3: INTERSECT Operator with WHERE ClauseQuery: SELECT employee_id, first_name, salary Explanation:
Output: ![]() Intersect Using WHERE Clause Explanation: As we can see below only the Minal is common in both the tables so it will return only that. Example 4: Intersecting Results of SubqueriesQuery: (SELECT employee_id, first_name, salary Output: ![]() Intersecting Results of Subqueries Explanation:
ConclusionThe Intersect operator in MariaDB provides a powerful means of finding common records between two or more queries. By understanding its syntax and usage, database developers and administrators can efficiently perform set operations. Intersect operator is help when we have large dataset to find out same record present in datasets. We can easily get the desired output. |
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |