SQLite Except Operator

  Total Views : 30
  Comments : 0
 
  Keywords : SQLite except clause with example, SQLite except in select statement with example, How to use sqlite except operator to exclude some of rows with example

Here we will learn SQLite Except operator with example and how to use SQLite Except operator to return all the rows from first select statement except the records returned by second select statement with examples. 

SQLite Except Operator

In SQLite Except operator is used to exclude some rows while returning from select statement. Suppose if we use Except operator with two SQLite Select statements then it will return all the records from first select statement except the records which exists in second Select statement.

 

Generally, we use SQLite Except operator with Select statements and all the Select statements which are using with Except operator must have same numer of fields.

 

Following is the pictorial representation of SQLite Except operator.

 

SQLite Except Operator Pictorial Representation with Example

 

If you observe above SQLite Except operator diagram it will return the records which are in color shaded area that means Except operator will return all the records from first Select statement except the records which exists in second Select statement.

Syntax of SQLite Except Operator

Following is the syntax of using SQLite Except operator with select statement.

 

SELECT column1, column2,..., columnn

FROM table-list

[WHERE condition]

 

EXCEPT

 

SELECT column1, column2,..., columnn

FROM table-list

[WHERE condition];

If you observe above SQLite Except operator syntax we defined two Select statements with Except operator. Here SQLite Except operator will return rows from first Select statement which are not exists in second Select statement. 

 

In above Except operator syntax we defined some of properties those are

 

columns - It may be no. of column or expression that you want as result.

 

table-list - It may be list of table from which you want result.

 

Where Condition - Its optional. We can use this condition based on our requirements in Select statements.

SQLite Except Operator Example

We will see how to use SQLite Except operator with SELECT statement for that first create one table called dept_master as follows.

 

CREATE TABLE dept_master

(dept_id INTEGER PRIMARY KEY AUTOINCREMENT,

dept_name TEXT);

 

INSERT INTO dept_master(dept_name)

VALUES('Admin'),

('Sales'),

('Quality Control');

('Marketing');

Same way create emp_master table and insert some records like as shown below.

 

CREATE TABLE emp_master

(emp_id INTEGER PRIMARY KEY AUTOINCREMENT,

first_name TEXT,

last_name TEXT,

salary NUMERIC,

dept_id INTEGER references dept_master);

 

INSERT INTO emp_master

values (1,'Honey','Patel',10100,1),

(2,'Shweta','Jariwala', 19300,2),

(3,'Vinay','Jariwala', 35100,3),

(4,'Jagruti','Viras', 9500,12);

Now we will see how to use SQLite Except operator with single column in Select statements for that write SQLite query like as shown following.

 

SELECT dept_id

FROM dept_master

 

EXCEPT

 

SELECT dept_id

FROM emp_master

When we run above sqlite Except operator query it will return dept_id from dept_master table which are not exists in emp_master table like as shown below.

 

dept_id

----------

4

SQLite Except with Multiple Columns

In case if we have multiple columns in SQLite Select statement then Except operator will use all the columns of first Select statement to compare values with second Select statement.

 

Following is the example of using SQLite Except operator with multiple columns in Select statement.

 

SELECT dept_id, dept_name

FROM table1

 

EXCEPT

 

SELECT dept_id, dept_name

FROM table2

Here above SQLite Except operator will return records of table1 which are not match with  dept_id, dept_name column values of table2.

 

This is how we can use SQLite Except operator to get records from Select statements based on our requirements.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User