SQL WHERE Clause


The SQL WHERE clause is used to extract / filter specific table records(rows).

It is used to specify a condition (one or more) at once while extracting / fetching the data from one or more tables.

If the given condition is matched successfully then only it returns filtered data or records from the table and which you expected to see.

The SQL WHERE clause is used in SELECT, UPDATE, INSERT, and DELETE statements.


Search Keys

  • sql where statement
  • sql multiple where
  • sql where or
  • where clause in sql
  • sql select where
  • sql where and
  • select from where
  • sql where in
  • sql where not
  • where condition
  • t sql where in
  • sql where having
  • sql where example
  • select from table where
  • sql multiple where clauses
  • sql join query with where clause
  • sql date in where clause
  • sql 2 where clauses
  • sql join with where clause example

SQL WHERE Clause Syntax

The below syntax is used to select specific column(s) and row(s) from the specific table.


SELECT 
column_name1, column_name2 
FROM table_name 
WHERE {column_name or expression} comparison-operator comparison-value;

The below syntax is used to select all column(s) and specific row(s) from the specific table.


SELECT * FROM table_name 
WHERE {column_name or expression} comparison-operator comparison-value;

  • column_name or expression - Is the column name of a table or a expression
  • comparison-operator - Comparision operators like =, <>, <=, >= etc.
  • comparison-value - Any user value for the given specific column or a column name for comparison

Note: The SQL WHERE clause can contains one or more conditions using AND or OR operators.


Sample Database Table - Books

BookId BookName BookPrice RelYear DomainName AuthorName
1 Professional MySql 115 2012 Database Devi Mai
2 The Complete Guide to SQL 190 2010 Database Siva Kumar
3 Oracle Cookbook 123.45 2009 Database Hari Krishnan
4 Securing SQL Server 65 2006 Security Vidyavathi
5 Programming with T-SQL for Beginners 80 2009 Database Devi Mai
6 Database Management 199.97 2012 Programming Pandurengan
7 Microsoft SQL Server 2008 168.27 2006 Security Hari Krishnan
8 Troubleshooting Oracle 71.87 2009 Security Geetha
9 A Visual Introduction To Sql 105 2006 Database Keshavan
10 Sql Server T-sql By Example 195 2015 Database Nirmala
11 SQL Made Simple... By Examples 180 2008 Database Vinoth Kumar
12 Sql Server Interview Questions 135 2008 Database Vinoth Kumar

Text Column Vs Numeric Column

SQL requires single or double (most database support) quotes around text or string values. However, we write numeric fields without quotes.


SQL WHERE Keyword Example - Text Field

The following SQL SELECT statement selects all the books from author "Hari Krishnan", in the "Books" table:


SELECT * FROM Books 
WHERE AuthorName = 'Hari Krishnan';

Note: In the above query, we have given single quotes for the value. Because "AuthorName" is text column.

The result of above query is:

BookId BookName BookPrice RelYear DomainName AuthorName
3 Oracle Cookbook 123.45 2009 Database Hari Krishnan
7 Microsoft SQL Server 2008 168.27 2006 Security Hari Krishnan

SQL WHERE Statement Example - Numeric Field

The following SQL SELECT statement selects all the books from price less than "100", in the "Books" table:


SELECT * FROM Books 
WHERE BookPrice < 100;

Note: In the above query, we have not given single quotes for the value. Because the "BookPrice" is a numeric column.

The result of above query is:

BookId BookName BookPrice RelYear DomainName AuthorName
4 Securing SQL Server 65 2006 Security Vidyavathi
5 Programming with T-SQL for Beginners 80 2009 Database Devi Mai
8 Troubleshooting Oracle 71.87 2009 Security Geetha

Search Keys

  • sql select where not in
  • sql select from where example
  • sql where clause examples
  • sql case in where clause example
  • sql multiple where conditions
  • sql where not in select
  • sql where 2 conditions
  • sql where multiple
  • sql where operators
  • sql select where in list
  • where between sql
  • where date sql
  • where if sql
  • multiple where sql
  • sql select where in list of values
  • sql select where between
  • sql query multiple where
  • sql multiple conditions
  • sql multiple select