Here we will learn sqlite and, or operators with example and how to use sqlite and operators, sqlite or operators and sqlite and & or operators in sqlite select or insert or update or delete statements with example
In SQLite AND & OR operators are used to perform multiple conditions on select, insert, update and delete statements based on our requirements.
We will learn how to use SQLite AND Operator, SQLite OR Operator and SQLite AND and OR operators in detailed with examples.
In SQLite AND operator will return rows or records which satisfy the conditions defined by using AND operator. Generally, we use SQLite AND operator to define multiple conditions in where clause.
Following is the syntax of SQLite AND operator to get records which satisfy conditions defined in select or insert or update or delete statements.
WHERE condition1 AND condition2 ...AND condition_n;
In above sqlite and operator syntax it will return records once all defined conditions become true.
We will see how to use SQLite AND operator with example before that first create emp_master table and insert some data to perform operations by using the following queries.
CREATE TABLE emp_master
(emp_id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name TEXT,
last_name TEXT,
salary NUMERIC,
dept_id INTEGER);
INSERT INTO emp_master
values (1,'Honey','Patel',10100,1),
(2,'Shweta','Jariwala', 19300,2),
(3,'Vinay','Jariwala', 35100,3),
(4,'Jagruti','Viras', 9500,2),
(5,'Shweta','Rana',12000,3),
(6,'sonal','Menpara', 13000,1),
(7,'Yamini','Patel', 10000,2),
(8,'Khyati','Shah', 50000,3),
(9,'Shwets','Jariwala', 19400,2);
Now run the following query to check records of emp_master table.
sqlite> SELECT * FROM emp_master;
emp_id first_name last_name salary dept_id
---------- ---------- ---------- ---------- ----------
1 Honey Patel 10100 1
2 Shweta Jariwala 19300 2
3 Vinay Jariwala 35100 3
4 Jagruti Viras 9500 2
5 Shweta Rana 12000 3
6 Sonal Menpara 13000 1
7 Yamini Patel 10000 2
8 Khyati Shah 50000 3
9 Shwets Jariwala 19400 2
Now write SQLite Query to use AND operator with select statement to get rows from emp_master table based on our conditions.
SELECT *
FROM emp_master
WHERE salary>1000 AND last_name='patel';
Here it will fetch all the records of emp_master table whose last name is patel and salary is greater than 1000 like as shown below.
emp_id first_name last_name salary dept_id
---------- ---------- ---------- ---------- ----------
1 Honey Patel 10100 1
This is how we can use SQLite AND condition to get records or rows based on our requirements.
The SQLite OR condition is useful to define multiple conditions in SQLite statements and it will return rows or records from the statement if any of one condition satisfied.
Following is the syntax of SQLite OR operator to return records even if one condition is satisfied.
WHERE condition1 OR condition2 ...OR condition_n;
In the above SQLite OR operator syntax, we defined multiple conditions with OR operator. In this if all the defined conditions satisfied means it will return records based on all conditions otherwise if anyone condition satisfies also it will return records based on that condition alone.
Following is the example SQLite OR Operator with Select statement to get records or rows from a table based on the defined conditions.
SELECT *
FROM emp_master
WHERE salary>15000 OR last_name='Patel';
Here it will fetch all the records of emp_master table whose last name is patel or whose salary is greater than 15000 like as shown below.
emp_id first_name last_name salary dept_id
---------- ---------- ---------- ---------- ----------
1 Honey Patel 10100 1
2 Shweta Jariwala 19300 2
3 Vinay Jariwala 35100 3
7 Yamini Patel 10000 2
8 Khyati Shah 50000 3
9 Shwets Jariwala 19400 2
In SQLite we can use both AND and OR operators together in SELECT, INSERT, UPDATE, or DELETE statement to perform required operations.
When combining AND and OR operators in SQLite, it is important to use parentheses so that the database knows what order to evaluate each condition.
Following is the syntax of using SQLite AND & OR together.
WHERE condition1
AND condition2
...
OR condition_n;
If you observe above SQLite statement, we defined few properties those are
condition1, condition2, ... condition_n - All of the conditions that must be met for the records to be selected.
If we use AND & OR operators together in SQLite, then
Following is the example of using both SQLite AND and OR operators in a select statement.
SELECT *
FROM emp_master
WHERE (last_name ='Patel' AND first_name ='Yamini')
OR (emp_id >= 5);
If you observe SQLite AND & OR example we defined AND & OR conditions by separating parentheses and it will return all the employees whose is having a last_name as 'Patel' and a first_name as 'Yamini', and all the employees who are having an emp_id greater than 5.
emp_id first_name last_name salary dept_id
---------- ---------- ---------- ---------- ----------
5 Shweta Rana 12000 3
6 Sonal Menpara 13000 1
7 Yamini Patel 10000 2
8 Khyati Shah 50000 3
9 Shwets Jariwala 19400 2
This is how we can use SQLite AND and OR operator with SQLite statements to get the required data based on our conditions.