![]() |
SQLite is a server-less database engine written in C programming language. It is developed by D. Richard Hipp in the year 2000. The main motive for developing SQLite is to escape from complex database engines like MySQL etc. It has become one of the most popular database engines as we use it in Television, Mobile Phones, Web browsers, and many more. It is written simply so that it can be embedded into other applications. In this article, you will learn about the SQLite Except operator, its working, and its functionality by using some examples. We will perform various queries with the practical implementation for an in-depth understanding of the article. SQLite Except OperatorSQLite EXCEPT operator is applied on multiple SELECT statements. It retrieves only the output of the first select statement and the output of the second select statement is not considered in the result set as we are using the EXCEPT operator. To apply Except operator the below two conditions must be satisfied:
Syntax: SELECT expression1, expression2, ... expression_n Explanation of Syntax: In the syntax, mean we have two select statements followed by the expressions which means the column names from which you are going to fetch the data and where optional condition followed by the except operator. The below image tells you how the SQLite Except operator works. ![]() Except operator If we clearly observe above in the image then we can understand that the here in the image, the SQLite EXCEPT operator diagram will return the records which are in a color shaded area that means Except operator will return all the records from first Select statement except the records which exist in second Select statement. Examples of SQLite EXCEPT OperatorTo understand the EXCEPT Operator in a better we need 2 table on which we will perform some operations and queries for good understanding. Here we have 2 tables called departments and employee table. If you don’t know How to Create a table in SQLite then refer this. The department table consist of dept_id and dept_name as Columns. After inserting some data into the table the department table looks: ![]() department table The employee table consist of dept_id,emp_id, first_name, and last_name as Columns. After inserting some data into the table the department table looks: ![]() employee table Example 1: EXCEPT Operator With Single ExpressionLet’s Find all department IDs present in the ‘department’ table but lacking any associated employees in the ‘employee‘ table. In this example we are going to retrieve only single expression from the table. Query: SELECT dept_id Output: ![]() SQLite Except Operator Explanation: In the above Query, We have fetch the department Id and employee id from the department and employee table on which EXCEPT operator is applied. However the employee id is not going to. In the output you can see that six rows are fetched and the column retrieved is department id that is resulted from the first select statement. Example 2: EXCEPT Operator With Multiple ExpressionLet’s find out all the employees first name and lastname frIn this example we are going to retrieve multiple expressions from the tables. Query: SELECT last_name, first_name Output: ![]() Except Multi Expression Explanation: In the above Query, We have fetch the last name, first name from the employee table and however the department id and department name are not going to fetch as the EXCEPT operator is applied. As we have learnt that EXCEPT operator .You can see that last name and first name columns are retrieved from the employee table as it is the first select statement. SQLite EXCEPT Operator Using ORDER By ClauseWe are going to use order by clause with the except operator. ORDER By clause is used to arrange the result set in the certain order. Syntax: SELECT expression1, expression2, ... expression_n Explanation od Syntax: In the query, our main focus to find and display a list of data (expressions) from specific tables. It removes any matching data from another selection based on defined conditions (WHERE clauses). Finally, it sorts the remaining results in a given order using ORDER BY clause. Query: Let’s retrieve a list of employee names as their first name and last name in descending order of last name, excluding those associated with certain departments. SELECT last_name, first_name Output: ![]() Except using order by Explanation: Here in the output you can see that last name and first name columns are retrieved in the descending order. ConclusionSQLite EXCEPT operator is used to retrieve only the first select statement data as the data of the second select statement is not fetched as we use the EXCEPT operator. Moreover the count of columns in both the select statements must be same and the data type too. It can be used only when you want to fetch the first select statement data. Witht the help of EXCEPT Operator, it returns only distinct rows, automatically removing any duplicates from the output. So EXCEPT Operator is maintain the performance optimization. |
Reffered: https://www.geeksforgeeks.org
Databases |
Related |
---|
![]() |
![]() |
![]() |
|
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |