Technology – What Are The SQL Minus and Except Clauses

Structured Query Language (SQL) is the de-facto query language used in most database management systems (DBMS) such as Oracle and Microsoft SQL Server. This domain-specific language is used in programming to query and return the desired data from a database. We use SQL to write queries that declare what data to expect from a dataset without really indicating how to obtain it. We can also use SQL to update and delete information from a database.

Ideally, in a relational database management system, the database usually runs on the “back end” side of a server in its structured form. By itself, this data is hard to interpret. So, users often have programs on a client computer that help to manipulate that data using rows, columns, fields, and tables. These programs are designed to allow users to send SQL statements to the server. The server then processes these statements by filtering data from the enormous, complex databases and returns results to the user.

Each query begins with finding the data needed then refining it down into something that can be processed and understood easily. To do this, you must use an organized set of operations to get meaningful data from a dataset. This article will explore the Minus Vs except SQL clauses to help you write optimized queries that run fast across various DBMS.

SQL EXCEPT clause

The SQL EXCEPT clause is one of the most commonly used statements that work together with two SELECT statements to return unique rows from a dataset. The SQL EXCEPT combines two SELECT statements to return the row that is present in the first select statement and not in the second.

If you’ve noticed, most SQL clauses do the same thing represented in standard spoken language. For instance, exception literally means not included. SQL EXCEPT is also very similar to the same concept.

The EXCEPT statement returns the distinct row from the left input query that is not output by the right input query. I.e., Returns the resultant rows that appear in query_expression_1 and not in query_expression_2.

SQL EXCEPT Clause Example

Consider a simple situation where you have two tables, one with dog names and the other one with cat names.

Cats Data Set

+———+———–+

| CatId | CatName |

|———+———–|

| 1 | Boss |

| 2 | Scarlet |

| 3 | Fluffy |

| 4 | Fluffy |

+———+———–+

Dogs Data Set

+———+———–+

| DogId | DogName |

|———+———–|

| 1 | Yelp |

| 2 | Woof |

| 3 | Boss |

| 4 | Boss |

+———+———–+

Using the SQL Except statement, we can filter the dataset and return only the distinct rows from the left SELECT query that have not been returned by the SELECT query on the right side of the EXCEPT statement.

An example SQL syntax query would look like this:

SELECT CatName FROM Cats

EXCEPT

SELECT DogName FROM Dogs;

In a typical scenario, a client program will send this query to the “back-end” server. This statement is then processed and only returns the values available in the “cats” dataset that don’t appear in the “dogs” dataset. When two rows are similar, as is the case with “fluffy,” only one row is returned. This is because the SQL query only returns distinct rows.

Here’s the result of the above query:

+———–+

| CatName |

|—————|

| Fluffy |

| Scarlet |

+————– +

Common SQL Except Rules
  • You must have the same number of columns in both queries
  • The column order must be the same in all queries
  • The column data types must be compatible with each other. The data types really don’t have to be the same, but they MUST be comparable through implicit conversion.
  • The EXCEPT statement returns all records from the 1st SELECT statement not available in the second SELECT statement
  • The EXCEPT operator in the SQL server is similar to the MINUS operator in Oracle.
  • MySQL does not support SQL Except clause. The workaround is to use the LEFT JOIN clause when using MySQL.

SQL MINUS Clause

MINUS operator does the same thing as the EXCEPT clause. But unlike the EXCEPT clause, the MINUS operator is only supported by Limit number of databases:

Database NameMinusExcept
Amazon RedshiftNoYes
VQL (denodo)YesNo
ElasticsearchYesNo
MariaDBYesYes
IBM Db2NoYes
Microsoft SQL ServerNoYes
MongoDBNoNo
MySQLNoNo
OracleYesNo
PostgreSQL No Yes
SOQL (salesforce)NoNo
snowflakeYesYes
SQLiteNoYes

The MINUS operator compares two queries and only returns the rows present in the first dataset but are not output by the second set. The result usually contains the distinct rows available in the left Select statement that aren’t included in the results of the right select statement.

Here is a typical MINUS syntax:

SELECT column_list_1 FROM T1

MINUS

SELECT column_list_2 FROM T2;

Common Oracle Minus Operator Rules

For MINUS operator to work, the dataset must conform with rules similar to those of SQL EXCEPT clause:

  • The data type of corresponding columns must be similar (Either Numeric or Character)
  • The order and number of columns must be the same.
  • The column used for ordering can be defined by the column number.
  • Duplicates are automatically eliminated in the final result.

Conclusion

The Minus Vs Except SQL Clause comparison can be confusing for many people. These two clauses are synonymous with each other and have similar syntax and results. Both Minus and Except help users skim through datasets to identify unique rows available only in the first SELECT query and not returned by the second SELECT query.

SQL Server Length Function Equivalent

The purpose of the Length function in SQL

The SQL LENGTH function returns the number of characters in a string. The LENGTH function is available in many Database Management Systems (DBMS).

The LENGTH Function Syntax

  • LENGTH(string)

LENGTH Function Notes

  • If the input string is empty, the LENGTH returns 0.
  • If the input string is NULL, the LENGTH returns NULL.

Length Function Across Databases

When working as a technical consultant, one has to work with customer’s databases and as you move from one database to another you will find that the function commands may vary–assuming the database has an equivalent function.

Working with VQL and SQL Server got me thing about the LENGTH() function, so, here is a quick references list, which does include the SQL Server.  

IBM DB2

  • LENGTH( )

IBM Informix

  • CHAR_LENGTH() Or CHARACTER_LENGTH()

MariaDB

  • LENGTH( )

Microsoft SQL Server

  • LEN( )

MySQL

  • CHAR_LENGTH() Or CHARACTER_LENGTH()

Netezza

  • LENGTH( )

Oracle

  • LENGTH( )

PostgreSQL

  • CHAR_LENGTH() Or CHARACTER_LENGTH()

SOQL (SalesForce)

  • SOQL has no LENGTH function

VQL (Denodo)

  • LEN( )

Oracle SQL Date Formatting Examples

Below is a SQL of various examples of Oracle date formats, which I have found to be a useful reference.

Select

SYSDATE,

TO_CHAR(SYSDATE,’YYYY-MM-DD AD’) AS ANNO_DOMINI,

TO_CHAR(SYSDATE,’YYYYMMDD’) AS SORT_PATTERN1,

TO_CHAR(SYSDATE,’YYYY/MM/DD’) AS DATE_PATTERN1,

TO_CHAR(SYSDATE,’DD/MM/YYYY’) AS DATE_PATTERN2,

TO_CHAR(SYSDATE,’DD/MON/YYYY’) AS DATE_PATTERN3,

TO_CHAR(SYSDATE,’DD-MON-YY’) AS DATE_PATTERN4,

TO_CHAR(SYSDATE,’CC’) AS CENTURY,

TO_CHAR(SYSDATE,’YYYY’) AS FOUR_DIGIT_YEAR,

TO_CHAR(SYSDATE,’YY’) AS YEAR_OF_CENTURY,

TO_CHAR(SYSDATE,’J’) AS JUALIAN_DATE,

TO_CHAR(SYSDATE,’Q’) AS CALENDAR_QUARTER_OF_YEAR,

TO_CHAR(SYSDATE,’WW’) AS CALENDAR_WEEK_OF_YEAR,

TO_CHAR(SYSDATE,’DDD’) AS NUMBER_OF_CALENDAR_DAY_OF_YEAR,

TO_CHAR(SYSDATE,’W’) AS WEEK_OF_MONTH,

TO_CHAR(SYSDATE,’DAY’) AS NAME_OF_DAY_OF_WEEK,

TO_CHAR(SYSDATE,’DY’) AS ABBREVIATED_DAY_OF_WEEK,

TO_CHAR(SYSDATE,’MM’) AS NUMERIC_MONTH_OF_YEAR,

TO_CHAR(SYSDATE,’MON’) AS ABBREVIATED_MONTH_OF_YEAR,

TO_CHAR(SYSDATE,’MONTH’) AS SPELLED_OUT_MONTH_OF_YEAR

FROM DUAL;

Related References

PS Query Timestamp to Date Logic

Converting a timestamp to date in PeopleSoft Query seems to be a common item, which I have helped others with, to the point that I kept a personal note on the coding logic.  So, I thought I would post this quick note in case it may be useful to someone.  This item would seem to be straight forward, however, PS query tries to help you with the process, so, some experimentation is usually involved to get the code to out just right.

Here is a sample expression I use for the timestamp to date conversion in PS Query.

  • Create an expression with an Expression Type of “Number
  • Set Length to 10
  • Sample code: TO_DATE(SUBSTR(<<RecordAlias.TimestampFieldName>>,1,10))
  • PS Query interpretation: TO_DATE(SUBSTR(TO_CHAR(CAST((<<RecordAlias.TimestampFieldName>>) AS TIMESTAMP),’YYYY-MM-DD-HH24.MI.SS.FF’),1,10))

Related References

PS Query Migration Validation SQLs

While PeopleSoft PeopleTools Application Designer is the best way to validate query migrations, sometimes a few SQL scripts can be helpful when verifying that queries and associated tree exist in an environment with the necessary nodes.  This SQL can be run in the appropriate SQL editor and was written against an Oracle RDBMS.

Is The Query in the Environment?

SELECT DISTINCT QRYNAME

FROM PSQRYFIELD

WHERE QRYNAME LIKE ’<<QUERYNAME>>%’;

Is Query Tree in Environment?

SELECT TREE_NAME

FROM PSTREENODE

WHERE TREE_NAME =’<<TREENAME>>’

ORDER BY 1;

Is Record in Query Security Tree?

SELECT TREE_NODE

FROM PSTREENODE

WHERE TREE_NAME =’<<TREENAME>>’

AND TREE_NODE LIKE ’%<<RecordName>>%’;

Related References

PeopleSoft XLAT Value Lookup SQL

I’ve had this SQL snippet laying around for a while, so, before I lose it, I thought I would post it for future reference.  Below is SQL to lookup a XLAT values, in case you don’t want to or cannot go through PeopleSoft Application Designer.

XLAT Lookup SQL

SELECT XLAT.FIELDNAME,

XLATFIELDVALUE,

XLAT.XLATLONGNAME

FROM PSXLATITEM XLAT

WHERE XLAT.FIELDNAME = ‘<<FieldName>>’

AND XLAT.EFF_STATUS = ‘A’

AND XLAT.EFFDT = ( SELECT MAX(XLAT1.EFFDT)

FROM PSXLATITEM XLAT1

WHERE  XLATFIELDNAME =XLAT1.FIELDNAME

AND XLATFIELDVALUE = XLAT1.FIELDVALUE)

ODER BY XLAT.FIELDVALUE