REQUEST COMPLIMENTARY SQLS*PLUS LICENCE
PostgreSQL INTERSECT statement
PostgreSQL INTERSECT statement returns the intersection of 2 or more data sets. Each data set is defined by the SELECT operator.
If a record exists in both datasets, it will be included in the INTERSECT results. However, if a record exists in one dataset rather than in the other, it will be omitted from the INTERSECT results.
INTERSECT query
Explanation: Request INTERSECT will return the records in the blue shaded area. These are the entries that exist in both select 1 and select 2.
Each SELECT statement in INTERSECT must have the same number of fields in the result sets with the same data types.
The syntax for the INTERSECT statement in PostgreSQL
SELECT expression1_id, expression2id,... expression_n_id
FROM tabs
[WHERE conds]
INTERSECT
SELECT expression1_id, expression2_id,... expression_n_id
FROM tabs
[WHERE conds];
Parameters and arguments of the statement
- expression1_id, expression2_id, expression_n_id – The Stollblock or the calculations you want to get.
- tabs – The tables from which you want to get the records. The FROM operator must specify at least one table.
- WHERE conds – Optional. These are the conditions that must be met to select records.
Note:
- Both SELECT operators must have the same number of expressions.
- The corresponding expressions must have the same data type in the SELECT operators.
For example, expression1_id must have the same data type in both the first and second SELECT operators.
Example of an INTERSECT statement with one expression
Below is an example of the INTERSECT operator, which has one field with the same data type:
SELECT category_id
FROM products
INTERSECT
SELECT category_id
FROM inventory;
In this example INTERSECT, if a category_id appears in both the products table and the inventory table, will appear in your resulting set.
Now let us complicate our example by adding WHERE conditions to the INTERSECT request.
SELECT category_id
FROM products
WHERE category_id < 800
INTERSECT
SELECT category_id
FROM inventory
WHERE quantity > 5;
In this example, WHERE has been added to each data set. The first dataset has been filtered out so that only those entries from the products table where category_id is less than 800 are returned. The second dataset has been filtered by entries from the inventory table where the quantity is larger than 5.
Example of an operator with several expressions
Below we will consider an example of using the INTERSECT operator in PostgreSQL to return more than one column.
For example:
SELECT contact_id, last_name, first_name
FROM contacts
WHERE last_name <> 'Ivanov'
INTERSECT
SELECT customer_id, last_name, first_name
FROM customers
WHERE customer_id < 100
;
In this example, the INTERSECT query will return records from the contacts table where contact_id, last_name, and first_name values correspond to customer_id, last_name, and first_name values from the customer’s table.
Each dataset has WHERE conditions for further filtering of the results so that only the records from contacts are returned where last_name does not equal ‘Ivanov’. Records from the customer_id table are returned where customer_id is less than 100.
Example of an operator using ORDER BY
Below is an example of INTERSECT, which uses ORDER BY :
SELECT contact_id, contact_name
FROM contacts
WHERE contact_id < 100
INTERSECT
SELECT company_id, company_name
FROM companies
WHERE state = 'Nevada'
ORDER BY 1;
Since the column names of the two SELECT operators are different, it is more advantageous to refer to the columns in ORDER BY by their position in the resulting set. In this example, we have sorted the results by contact_id / company_id in ascending order as ORDER BY 1.
The fields contact_id / company_id are at position #1 in the resulting set.
Intersect operator in SQL Server
MORE NEWS
PreambleNoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...
PreambleMongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...
5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...
The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...
PreambleMS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...
First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...
PreambleAtom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...
PreambleMongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...
FlexibilityOne of the most advertised features of MongoDB is its flexibility. Flexibility, however, is a double-edged sword. More flexibility means more...
PreambleSQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...
PreambleWriting an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...
PreambleOracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...
PreambleIBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...
What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....
PreambleIf you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...
PreambleStarting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...