codesnout.com

SQL IN

Google Ads

SQL IN


SQL Sample Code - SQL IN Operator.


SELECT column_name(s)
FROM table_name WHERE column_name IN (value1,value2,value3...)

See the SQL samples section below for an explanation of the IN operator.


What does the SQL IN operator do?

SQL IN operator used as part of a WHERE clause works the same as the OR operator except it is easier to write.

As with the OR operator, IN uses the conditions specified in the WHERE part of the statement. The IN operator retrieves the records which matches either IN conditions in the statement.


Sample – SQL IN

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 Pisa.

Use the SELECT statement below:

SELECT * FROM tblCompany WHERE Town IN (‘Pisa’, ’Hamburg’);

Use the SELECT statement below:

The table of results will look like this:

Company_ID CompanyName Address Town Sales
1 SQL Sample 1 Sample St Hamburg 10000
2 SQL Code Land 2 Code Rd Hamburg 20000
4 SQL Reference Ltd 34 Reference St Pisa 17000

This could have been written using the OR operator as follows;

SELECT * FROM tblCompany WHERE Town=‘Pisa’ OR 'Hamburg';

 


Related SQL Sample Code:


About Us | Privacy Policy | Contact Us | ©2010 Thunderousity Information Management Solutions | www.autosnout.com - Car Performance Statistics Website Utilising SQL | SQL Blog |

SQL Home | SQL Intro | SQL SELECT Statement | DISTINCT | WHERE | SQL Wildcards | AND OR | IN | BETWEEN | LIKE | ORDER BY | GROUP BY | TOP | ALIAS | DELETE | INSERT | UPDATE | CREATE DATABASE | CREATE TABLE | ALTER | INNER JOIN | FULL JOIN | LEFT JOIN | RIGHT JOIN | UNION | CONSTRAINTS | NOT NULL| Aggregate Functions | SUM | AVG | COUNT | MAX | MIN | FIRST | LAST | GROUP BY | HAVING | LIMIT | Microsoft Access Data Types | MySQL Data Types | SQL Server Data Types | ASCII HTML Reference Sheet |