Today's Offer - SQL Server Certification Training - Enroll at Flat 10% Off.

- SQL Server Blogs -

SQL Where Clause with Examples

The SQL Where Clause is used to specify a condition while fetching data from a single table or multiple tables are combined together. When a particular condition is satisfied, it returns the desired value from the table. The Where clause should be used to filter records and to fetch only necessary records from the table. The WHERE clause in SQL can be used along with all statements like SELECT, UPDATE, INSERT, etc.

The basic syntax of Where clause in SQL can be given as:


SELECT column1, column2, columnN'
FROM table_name
WHERE [condition]

You can make the condition more specific by using different comparison or logical operators like equal to, not, etc. Here is a list of comparison operators in SQL.

Operator Description
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal
BETWEEN Defines a certain range
LIKE Search for a pattern
IN To specify multiple possible values for a column

SQL Server Curriculum

Why do we need Where Clause in SQL?

The SELECT statement in SQL is used to query data from a database, but it returns all records from the queried database. However, there is a time when we have to restrict the query result to a specified condition. It is when Where clause in SQL comes handy. A simple syntax using SELECT and Where clause can be written as:

SELECT * FROM tableName WHERE Condition;

Here, SELECT is the most common statement that is used to fetch the data from the table. The WHERE keyword restricts the query result and condition is the filter applied to the query results. The filter could be a specific range or value or sub-query. Here is one quick example for your reference, how to specific condition using Where clause in SQL.

SELECT * FROM 'members' WHERE 'membership_number' = 1;

Take an example where we want to give personal details of a member whose membership number is one then we can use the given syntax for the same purpose. The objective of Where clause in SQL is:

  • To limit the number of rows using the WHERE clause.
  • It is used to filter rows that meet a specific criterion.
  • Where clause is followed by a particular condition that returns True or False.
  • Where clause can be used with multiple SQL statements like INSERT, SELECT, UPDATE, etc.

A WHERE clause with SELECT statement looks like:


SELECT column-names
FROM table-name
WHERE condition

The SELECT statement in SQL is used to query a database that lists all the records from a table. If you want to restrict your research, then you should combine it with other SQL clauses and WHERE clause is one of the most common examples that restricts the query result based on a specified condition. If you want to work with SQL databases, then it is necessary having hands-on expertize on SELECT and WHERE clauses otherwise things will not work well for you.

A WHERE clause with UPDATE statement looks like:


UPDATE table-name
SET column-name =value
WHERE condition

One of the most popular examples when working with SQL is an Update command that is used to update the existing records within a Table. This is an action query that can be applied to a set of records based on conditions you specify. This is taken as one of the most powerful features of RDBMS because you have the flexibility to modify multiple records together. A depth understanding of the Update Query helps you in improving the performance of a database application instead of performing all changes one by one manually in the code. Also, the maintenance of changes is much easier than you ever think of.

With update command in SQL, you can work on multiple records together and WHERE clause in between to specify some condition. Here, are the possible uses of Update Query within a database –

Read: How to Become a SQL Database Administrator?
  • It can be used to set the same values for all the records.
  • A value from another table to your table.
  • A value from the field to the linked table.
  • A value derived from some expression.
  • One VBA function where field values can be used as parameters
  • One customized VBA function where parameters could be set NULL

A WHERE clause with DELETE statement looks like:


DELETE table-name
WHERE Condition


The DELETE statement in SQL is used to delete a table or particular entry from the table based on the specified condition. It will only delete the entries from the table, not the table schema. If you want to delete the entire table from the database along with its structure, then you should use the truncate commands in this case.

Combining SQL clauses with Operators

SQL Operators are used to specifying conditions within an SQL statement and work as a conjunction to join various conditional statements. You can also use operators in SQL statements. Here is a list of operators that are frequently used by SQL statements:

  • Comparison Operators
  • Logical Operators
  • Negate Condition Operators
  • Arithmetic Operators

SQL Comparison Operators

There is a list of comparison operators in SQL that are listed below:

  • Greater Than OR “>”
  • Less Than OR “<”
  • Equal to operator OR “=”
  • Not Equal To OR “!=” OR “<>”
  • Greater Than Equal to OR “>=”
  • Less Than Equal to OR “<=”

SQL Logical Operators

Following listed logical operators are frequently used by the SQL statements:

  • AND
  • BETWEEN
  • ANY
  • NOT
  • LIKE
  • EXISTS
  • IN
  • OR
  • UNIQUE

SQL Server quiz

SQL Arithmetic Operators

For two variables of numeric values, we can use the following arithmetic operators:

Following arithmetic operators are used by the SQL statements:

  1. Addition  “+”
  2. Subtraction “-”
  3. Multiplication “*”
  4. Division “/”
  5. Modulus “%”

With the help of SQL queries, we may choose specific data from bulk data files. It is obvious that we cannot store the whole data in a single data but we should use multiple tables to store data logically. You can combine data from multiple tables to a single table or perform operations on multiple tables as required. It is all possible with the help of SQL operators. Moving ahead, let us see how where clause and operators can be used together.

SQL WHERE clause with AND logical Operator

A WHERE clause in SQL can be used with AND operator, if all filter criterion specified, are met. Consider an example where we want to get the complete list of movies that were released in 2008. You can use the following query for this purpose:

SELECT * FROM 'movies' WHERE 'category_id' = 2 AND 'year_released' = 2008 ;

Here, we can combine two or more conditions to make the query result more specific and accurate. For the above example, it has to meet two conditions, one is category ID and other is movie released time. The other common operator that we can use with WHERE clause is OR logical operator.

SQL WHERE clause with OR logical Operator

Here is one operator that can be used to WHERE clause in SQL. For AND operator, both conditions should be justified to process the final value. At the same time, when we are using the OR operator, even if one condition meets, the final value is calculated. Here is a quick example for your reference.

SELECT * FROM 'movies' WHERE 'category_id' =1 OR 'category_id' = 2 ;

Read: Delete vs Truncate SQL Server – What are the Differences?

The following query will process all records either from category 1 or category 2. If we are using AND operator, then the movie name should be given in both the categories. These operators can be combined with queries as per the requirement.

SQL WHERE clause with IN logical Operator

The WHERE clause when combined with IN operator in SQL, it will fetch results whose values are given within the IN keywords. It makes the result more specific but redundant sometimes. Here is one quick example for your reference.

SELECT * FROM 'members' WHERE 'membership_number' IN (1,2,3);

The given query will fetch records whose membership number is either 1,2 or 3. In the same way, you can define category ID or column name as per the requirement.

SQL WHERE clause with NOT IN logical Operator

The WHERE clause when combined with NOT IN operator in SQL, it will not affect the rows whose values are given within the NOT IN keyword. It makes the result more specific but redundant sometimes. It may create confusion so try using it carefully. Here is one quick example for your reference.

SELECT * FROM 'members' WHERE 'membership_number' NOT in (1,2,3);

The given query will fetch records whose membership number is not 1,2 or 3.

SQL WHERE clause with Comparison Operators

Equal to, less than, not equal to, are a few comparison operators that can be used with the WHERE clause.

EQUAL TO (=)

SELECT * FROM 'members' WHERE 'gender' = 'female';

The given query will fetch all records whose gender is equal to the female. This is a common operator that can be used frequently as per the requirement.

GREATER THAN (>)

Read: What is SQL Server Replication and How it Works?

Here, you can fetch records whose value is greater than the given value. For example, you want to fetch records where payment paid is greater than 2000. For this purpose, you can use the following query.

SELECT * FROM 'payments' WHERE 'amount_paid' > 2000;

It will quickly show a list of payment that is above 2000 and easy to analyze by anyone. In this way, you can save your time where you don’t have to analyze the whole table, but a few entries can work great for you.

NOT EQUAL TO (<>)

SELECT * FROM 'movies' WHERE 'category_id' <> 1;

The given query selects movies whose category ID is not equal to one. It works opposite to the Equal to an operator.

free SQL Server demo

Summary

  • The SQL Where Clause is used to specify a condition while fetching data from a single table or multiple tables are combined together. A WHERE clause in SQL can be used along with all statements like SELECT, UPDATE, INSERT, etc.
  • SELECT statement in SQL is used to fetch all the records from a table. The UPDATE statement is used to update records within a table. The INSERT statement is used to insert values to a table. And DELETE statement is used to delete records from a table. When they are combined with WHERE clause, it makes the query result more accurate and specific.
  • The WHERE clause can be used in conjunction with logic operators like AND, OR, IN, etc. The WHERE clause can be used in conjunction with comparison operators like Equal to, Not Equal to, Less than, Greater than, etc.

Final Words:

The blog for SQL where clause gave you a perfect idea of how to use the clause within the query. It tells you about the meaning and necessity of WHERE clause and how to use it in the best way with examples. Also, it explains to you what will happen when the WHERE clause is combined with logical and comparison operators.

Once you have gone through all the topics, it is the time to practice certain problems and enhance your skillset. For this purpose, you can join the SQL certification course with JanBask Training and get certified with us. We help you in becoming a valuable IT resource for whom getting jobs with attractive salary packages is easier. All the Best!

Read: SAS Tutorial Guide for Beginners

    Janbask Training

    JanBask Training is a leading Global Online Training Provider through Live Sessions. The Live classes provide a blended approach of hands on experience along with theoretical knowledge which is driven by certified professionals.


Trending Courses

AWS

  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53

Upcoming Class

4 days 24 Nov 2019

DevOps

  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing

Upcoming Class

5 days 25 Nov 2019

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning

Upcoming Class

5 days 25 Nov 2019

Hadoop

  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation

Upcoming Class

6 days 26 Nov 2019

Salesforce

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

-1 day 19 Nov 2019

Course for testing

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

34 days 24 Dec 2019

QA

  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing

Upcoming Class

13 days 03 Dec 2019

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum

Upcoming Class

5 days 25 Nov 2019

SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design

Upcoming Class

-1 day 19 Nov 2019

Comments

Search Posts

Reset

Receive Latest Materials and Offers on SQL Server Course

Interviews