1.

What are UNION, MINUS and INTERSECT commands?

Answer»

The UNION operator combines and returns the result-set RETRIEVED by two or more SELECT statements.
The MINUS operator in SQL is used to remove duplicates from the result-set obtained by the second SELECT query from the result-set obtained by the first SELECT query and then RETURN the filtered RESULTS from the first.
The INTERSECT clause in SQL combines the result-set fetched by the two SELECT statements where records from ONE match the other and then returns this intersection of result-sets.

Certain conditions need to be met before executing either of the above statements in SQL -

  • Each SELECT statement within the clause must have the same number of columns
  • The columns must also have similar data types
  • The columns in each SELECT statement should necessarily have the same order
SELECT name FROM Students /* Fetch the union of queries */UNIONSELECT name FROM Contacts;SELECT name FROM Students /* Fetch the union of queries with duplicates*/UNION ALLSELECT name FROM Contacts;SELECT name FROM Students /* Fetch names from students */MINUS /* that aren't present in contacts */SELECT name FROM Contacts;SELECT name FROM Students /* Fetch names from students */INTERSECT /* that are present in contacts as well */SELECT name FROM Contacts; Write a SQL query to fetch "names" that are present in either table "accounts" or in table "registry". Check Write a SQL query to fetch "names" that are present in "accounts" but not in table "registry". Check Write a SQL query to fetch "names" from table "contacts" that are neither present in "accounts.name" nor in "registry.name". Check


Discussion

No Comment Found