WHERE Clause in SQL - sql - sql tutorial - learn sql



  • The SQL WHERE clause is used to specify a condition while fetching the data from a single table or by joining with multiple tables.
  • If the given condition is satisfied, then only it returns a specific value from the table.
  • You should use the WHERE clause to filter the records and fetching only the necessary records.
  • The WHERE clause is not only used in the SELECT statement, but it is also used for
    • SELECT
    • UPDATE
    • DELETE

Syntax

  • The syntax for using WHERE in the SELECT statement is as follows:
SELECT "column_name"
FROM "table_name"
WHERE "condition";
  • The syntax for using WHERE in the UPDATE statement is as follows:
UPDATE "table_name"
SET "column_1" = [new value]
WHERE "condition";
  • The syntax for using WHERE in the DELETE statement is as follows:
DELETE FROM "table_name"
WHERE "condition";
  • "Condition" can include a single comparison clause (called simple condition) or multiple comparison clauses combined together using AND or OR operators (compound condition).
Tags : sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Examples

  • We provide examples here to show how to use WHERE in the SELECT statement.

Example 1: WHERE Clause With Simple Condition

  • To select all stores with sales above $1,000 in Table Store_Information,

Table Store_Information

Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
Los Angeles 300 Jan-08-1999
Boston 700 Jan-08-1999
  • we key in,
SELECT Store_Name FROM Store_Information  WHERE Sales > 1000;

Result:

Store_Name
Los Angeles

Example 2: WHERE Clause With OR Operator

  • To view all data with sales greater than $1,000 or with transaction date of 'Jan-08-1999', we use the following SQL,
SELECT *FROM Store_Information WHERE Sales > 1000 OR Txn_Date = 'Jan-08-1999';

Result:

Store_Name Sales Txn_Date
Los Angeles 500 Jan-05-1999
Los Angeles 300 Jan-08-1999
Boston 700 Jan-08-1999
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

Using WHERE With UPDATE and DELETE

  • As mentioned above, the WHERE clause can be used with UPDATE and DELETE statements in addition to the SELECT statement. Examples of how to use the WHERE clause with these two commands can be seen in the UPDATE and DELETE sections.

Interview questions for where clause in SQL:

  • For these exercises, assume we have a table called Users with the following data:

Table Users

First_Name Last_Name Birth_Date Gender Join_Date Total_Sales
Sophie Lee Jan-05-1960 F Apr-05-2015 500
Richard Brown Jan-07-1975 M Apr-05-2015 200
Jamal Santo Oct-08-1983 M Apr-09-2015 350
Casey Healy Sep-20-1969 M Apr-09-2015 80
Jill Wilkes Nov-20-1979 F Apr-15-2015 210
  • Which of the following SQL statement is valid? (There can be more than one answer)
  1. ) SELECT * FROM Users WHERE Gender = 'M'; 
    
  2. SELECT * WHERE Gender = 'M' FROM Users; 
    
  1. SELECT Gender= 'M' FROM Users; 
  2. SELECT Gender FROM Users WHERE Last_Name = 'Wilkes';
    
  • Answer: a) and d)
  • What's the result of the following query?
	SELECT * FROM Users WHERE Join_Date = 'Apr-09-2015';
  • Answer: The result is,
First_Name Last_Name Birth_Date Gender Join_Date Total_Sales
Jamal Santo Oct-08-1983 M Apr-09-2015 350
Casey Healy Sep-20-1969 M Apr-09-2015 80
  • (True or False) The condition used in the WHERE clause must include a column that is part of the SELECT clause.
  • Answer: False. The column used in the WHERE clause can be any column in the table. It is not necessary for the column to be part of the SELECT clause.
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

SQL Where Condition Sample Application

SQL Where Condition Sample Application
sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

SQL Sample Application WHERE

SQL Sample Application WHERE

This tutorial provides more the basic needs and informations on sql tutorial , pl sql tutorial , mysql tutorial , sql server , sqlcode , sql queries , sql , sql formatter , sql join , w3schools sql , oracle tutorial , mysql , pl sql , learn sql , sql tutorial for beginners , sql server tutorial , sql query tutorial , oracle sql tutorial , t sql tutorial , ms sql tutorial , database tutorial , sql tutorial point , oracle pl sql tutorial , oracle database tutorial , oracle tutorial for beginners , ms sql server tutorial , sql tutorial pdf

Related Searches to WHERE Clause in SQL