top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

SQL: MINUS Operator

0 votes
358 views

This SQL tutorial explains how to use the SQL MINUS operator with syntax and examples.

Description

The SQL MINUS operator is used to return all rows in the first SELECT statement that are not returned by the second SELECT statement. Each SELECT statement will define a dataset. The MINUS operator will retrieve all records from the first dataset and then remove from the results all records from the second dataset.

Minus Query

SQL

Explanation: The MINUS query will return the records in the blue shaded area. These are the records that exist in Dataset1 and not in Dataset2.

Each SELECT statement within the MINUS query must have the same number of fields in the result sets with similar data types.

TIP: The MINUS operator is not supported in all SQL databases. It can used in databases such as Oracle.

For databases such as SQL Server, PostgreSQL, and SQLite, use the EXCEPT operator to perform this type of query.

Syntax

The syntax for the MINUS operator in SQL is:

SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
MINUS
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];

Parameters or Arguments

expression1, expression2, expression_n

The columns or calculations that you wish to retrieve.

tables

The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.

WHERE conditions

Optional. These are conditions that must be met for the records to be selected.

Note

  • There must be same number of expressions in both SELECT statements.
  • The corresponding expressions must have the same data type in the SELECT statements. For example: expression1 must be the same data type in both the first and second SELECT statement.

Example - With Single Expression

The following is a SQL MINUS operator example that has one field with the same data type:

SELECT supplier_id
FROM suppliers
MINUS
SELECT supplier_id
FROM orders;

This SQL MINUS example returns all supplier_id values that are in the suppliers table and not in the orders table. What this means is that if a supplier_id value existed in the suppliers table and also existed in the orders table, the supplier_id value would not appear in this result set.

Example - Using ORDER BY Clause

The following is a MINUS operator example that uses the ORDER BY clause:

SELECT supplier_id, supplier_name
FROM suppliers
WHERE supplier_id > 2000
MINUS
SELECT company_id, company_name
FROM companies
WHERE company_id > 1000
ORDER BY 2;

In this SQL MINUS operator example, since the column names are different between the two SELECT statements, it is more advantageous to reference the columns in the ORDER BY clause by their position in the result set. In this example, we've sorted the results by supplier_name / company_name in ascending order, as denoted by the ORDER BY 2.

The supplier_name / company_name fields are in position #2 in the result set.

posted Feb 22, 2017 by Shivaranjini

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

This SQL tutorial explains how to use the SQL INTERSECT operator with syntax and examples.

Description

The SQL INTERSECT operator is used to return the results of 2 or more SELECT statements. However, it only returns the rows selected by all queries or data sets. If a record exists in one query and not in the other, it will be omitted from the INTERSECT results.

Intersect Query

SQL

Explanation: The INTERSECT query will return the records in the blue shaded area. These are the records that exist in both Dataset1 and Dataset2.

Each SQL statement within the SQL INTERSECT must have the same number of fields in the result sets with similar data types.

Syntax

The syntax for the INTERSECT operator in SQL is:

SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
INTERSECT
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];

Parameters or Arguments

expression1, expression2, expression_n

The columns or calculations that you wish to retrieve.

tables

The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.

WHERE conditions

Optional. These are conditions that must be met for the records to be selected.

Note

  • There must be same number of expressions in both SELECT statements.
  • The corresponding expressions must have the same data type in the SELECT statements. For example: expression1 must be the same data type in both the first and second SELECT statement.

Example - With Single Expression

The following is a SQL INTERSECT operator example that has one field with the same data type:

SELECT supplier_id
FROM suppliers
INTERSECT
SELECT supplier_id
FROM orders;

In this SQL INTERSECT example, if a supplier_id appeared in both the suppliers and orders table, it would appear in your result set.

Now, let's complicate our example further by adding WHERE conditions to the INTERSECT query.

SELECT supplier_id
FROM suppliers
WHERE supplier_id > 78
INTERSECT
SELECT supplier_id
FROM orders
WHERE quantity <> 0;

In this example, the WHERE clauses have been added to each of the datasets. The first dataset has been filtered so that only records from the suppliers table where the supplier_id is greater than 78 are returned. The second dataset has been filtered so that only records from the orders table are returned where the quantity is not equal to 0.

Example - With Multiple Expressions

Next, let's look at an example of how to use the INTERSECT operator in SQL to return more than one column.

For example:

SELECT contact_id, last_name, first_name
FROM contacts
WHERE last_name <> 'Anderson'
INTERSECT
SELECT customer_id, last_name, first_name
FROM customers
WHERE customer_id < 50;

In this INTERSECT example, the query will return the records from the contacts table where the contact_idlast_name, and first_name values match the customer_idlast_name, and first_name value from the customers table.

There are WHERE conditions on each data set to further filter the results so that only records from the contacts are returned where the last_name is not Anderson. The records from the customers table are returned where the customer_id is less than 50.

Example - Using ORDER BY

The following is an INTERSECT example that uses a ORDER BY clause:

SELECT supplier_id, supplier_name
FROM suppliers
WHERE supplier_id > 2000
INTERSECT
SELECT company_id, company_name
FROM companies
WHERE company_id > 1000
ORDER BY 2;

Since the column names are different between the two SELECT statements, it is more advantageous to reference the columns in the ORDER BY clause by their position in the result set. In this example, we've sorted the results by supplier_name / company_name in ascending order, as denoted by the ORDER BY 2.

The supplier_name / company_name fields are in position #2 in the result set.

READ MORE

In SQL queries sometimes we need to round off some decimal  or float values, at that time we always think that which option to be applied while we have three different kinds of system defined SQL rounding functions-Ceiling, Floor and Round.

 

CEILING

Get the value on the right side of the decimal and returns the smallest integer greater or equal to, the specified values.

 

FLOOR

Get the value on the right side of the decimal and returns the largest integer less or equal to the specified values (only number)

 

ROUND

Rounds a positive or negative value to a specific length.

 

Example of SQL rounding functions i.e. floor, ceiling and round

 

 

Difference between Ceiling, Floor and Round in SQL Server

READ MORE

This SQL Server tutorial explains how to use the ROUND function in SQL Server (Transact-SQL) with syntax and examples.

Description

In SQL Server (Transact-SQL), the ROUND function returns a number rounded to a certain number of decimal places.

Syntax

The syntax for the ROUND function in SQL Server (Transact-SQL) is:

ROUND( number, decimal_places [, operation ] )

Parameters or Arguments

number

The number to round.

decimal_places

The number of decimal places rounded to. This value must be a positive or negative integer. If this parameter is omitted, the ROUND function will round the number to 0 decimal places.

operation

Optional. The operation can be either 0 or any other numeric value. When it is 0 (or this parameter is omitted), the ROUND function will round the result to the number of decimal_places. If operation is any value other than 0, the ROUND function will truncate the result to the number of decimal_places.

Note

  • If the operation parameter is 0 (or not provided), the ROUND function will round the result to the number of decimal_places.
  • If the operation parameter is non-zero, the ROUND function will truncate the result to the number of decimal_places.
  • See also the CEILING and FLOOR functions.

Applies To

The ROUND function can be used in the following versions of SQL Server (Transact-SQL):

  • SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005

Example

Let's look at some SQL Server ROUND function examples and explore how to use the ROUND function in SQL Server (Transact-SQL).

For example:

SELECT ROUND(125.315, 2);
Result: 125.320    (result is rounded because 3rd parameter is omitted)

SELECT ROUND(125.315, 2, 0);
Result: 125.320    (result is rounded because 3rd parameter is 0)

SELECT ROUND(125.315, 2, 1);
Result: 125.310    (result is truncated because 3rd parameter is non-zero)

SELECT ROUND(125.315, 1);
Result: 125.300    (result is rounded because 3rd parameter is omitted)

SELECT ROUND(125.315, 0);
Result: 125.000    (result is rounded because 3rd parameter is omitted)

SELECT ROUND(125.315, -1);
Result: 130.000    (result is rounded because 3rd parameter is omitted)

SELECT ROUND(125.315, -2);
Result: 100.000    (result is rounded because 3rd parameter is omitted)
READ MORE

This SQL Server tutorial explains how to use the FLOOR function in SQL Server (Transact-SQL) with syntax and examples.

Description

In SQL Server (Transact-SQL), the FLOOR function returns the largest integer value that is equal to or less than a number.

Syntax

The syntax for the FLOOR function in SQL Server (Transact-SQL) is:

FLOOR( number )

Parameters or Arguments

number

The value used to determine the largest integer value that is equal to or less than a number.

Note

  • See also the CEILING and ROUND functions.

Applies To

The FLOOR function can be used in the following versions of SQL Server (Transact-SQL):

  • SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005

Example

Let's look at some SQL Server FLOOR function examples and explore how to use the FLOOR function in SQL Server (Transact-SQL).

For example:

SELECT FLOOR(5.9);
Result: 5

SELECT FLOOR(34.29);
Result: 34

SELECT FLOOR(-5.9);
Result: -6
READ MORE

This SQL Server tutorial explains how to use the CEILING function in SQL Server (Transact-SQL) with syntax and examples.

Description

In SQL Server (Transact-SQL), the CEILING function returns the smallest integer value that is greater than or equal to a number.

Syntax

The syntax for the CEILING function in SQL Server (Transact-SQL) is:

CEILING( number )

Parameters or Arguments

number

The number used to find the smallest integer value.

Note

  • See also the FLOOR and ROUND functions.

Applies To

The CEILING function can be used in the following versions of SQL Server (Transact-SQL):

  • SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005

Example

Let's look at some SQL Server CEILING function examples and explore how to use the CEILING function in SQL Server (Transact-SQL).

For example:

SELECT CEILING(32.65);
Result: 33

SELECT CEILING(32.1);
Result: 33

SELECT CEILING(32);
Result: 32

SELECT CEILING(-32.65);
Result: -32

SELECT CEILING(-32);
Result: -32
READ MORE
...