SQL AND & OR |
SQL AND & OR Operators |
AND
| SELECT column_name(s) FROM table_name WHERE “Condition1” AND “Condition2”…; |
SQL Sample Code - SQL AND & OR Operators |
OR
| SELECT column_name(s) FROM table_name WHERE “Condition1” OR “Condition2”; |
SQL Sample Code - SQL AND & OR Operators |
AND & OR used together.
| SELECT column_name(s) FROM table_name WHERE “Condition1” AND (“Condition2” OR “Condition3”); |
See the SQL Samples section below for an explanation.
| What do the SQL AND & OR operator do? |
AND - The AND operator searches for multiple conditions specified in the WHERE part of the statement. The AND operator retrieves the records if those conditions are all true.
OR – The OR operator uses the conditions specified in the WHERE part of the statement. The OR Operator retrieves the records which matches either OR conditions in the statement.
SQL Sample 1 – SQL AND operator. |
In this SQL sample we will use the database table ‘tblCompany’.
| Company_ID | CompanyName | Address | Town | Sales |
| 1 | SQL Sample | 1 Sample St | Hamburg | 10000 |
| 2 | SQL Code Land | 2 Code Rd | Hamburg | 20000 |
| 3 | Sample Code World | 66 SQL St | Curry | 18000 |
| 4 | SQL Reference Ltd | 34 Reference St | Pisa | 17000 |
In this SQL sample we want to retrieve all the columns from the tblCompany table whose company is based in the town of Hamburg and have a Sales figure more than £15000.
Use the SELECT statement below:
| SELECT * FROM tblCompany WHERE Town=‘Hamburg’ AND Sales>15000; |
Use the SELECT statement below:
The table of results will look like this:
| Company_ID | CompanyName | Address | Town | Sales |
| 2 | SQL Code Land | 2 Code Rd | Hamburg | 20000 |
| 3 | Sample Code World | 66 SQL St | Curry | 18000 |
SQL Sample 2 – SQL OR operator. |
In this SQL sample we will use the database table ‘tblCompany’.
| Company_ID | CompanyName | Address | Town | Sales |
| 1 | SQL Sample | 1 Sample St | Hamburg | 10000 |
| 2 | SQL Code Land | 2 Code Rd | Hamburg | 20000 |
| 3 | Sample Code World | 66 SQL St | Curry | 18000 |
| 4 | SQL Reference Ltd | 34 Reference St | Pisa | 17000 |
In this SQL sample we want to retrieve all the Columns from the tblCompany table whose company is based in the town of Hamburg or have a Sales figure of less than £15000.
Use the SELECT statement below:
| SELECT * FROM tblCompany WHERE Town=‘Pisa’ OR Sales<15000; |
The table of results will look like this:
| Company_ID | CompanyName | Address | Town | Sales |
| 1 | SQL Sample | 1 Sample St | Hamburg | 10000 |
| 4 | SQL Reference Ltd | 34 Reference St | Pisa | 17000 |
SQL Sample 3 – Using AND & OR together. |
In this SQL sample we will use the database table ‘tblCompany’.
| Company_ID | CompanyName | Address | Town | Sales |
| 1 | SQL Sample | 1 Sample St | Hamburg | 10000 |
| 2 | SQL Code Land | 2 Code Rd | Hamburg | 20000 |
| 3 | Sample Code World | 66 SQL St | Curry | 18000 |
| 4 | SQL Reference Ltd | 34 Reference St | Pisa | 17000 |
In this SQL sample we want to retrieve all the Columns from the tblCompany table whose company is based in the town of Hamburg or have a Sales figure of less than £15000.
Use the SELECT statement below:
| SELECT * FROM tblCompany WHERE Town = ‘Hamburg’ AND (Sales=10000 OR Sales=9000); |
The table of results will look like this:
| Company_ID | CompanyName | Address | Town | Sales |
| 1 | SQL Sample | 1 Sample St | Hamburg | 10000 |
Related SQL Sample Code:
|
|