C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs



Home » SQL

Difference between WHERE and HAVING clauses in SQL






What are the differences between WHERE and HAVING clauses? Here, we are going to learn about what are the similarities and dissimilarities between HAVING and WHERE clauses in SQL?
Submitted by Preeti Jain, on April 02, 2018

WHERE Clause

  • WHERE Clauses can be used for conditional
  • It is basically to fetch a particular record
  • If we are using WHERE Clauses then WHERE Clauses should be mention before having clauses
  • WHERE Clauses cannot be implemented on grouping
  • We should implement WHERE Clauses whenever condition required

Syntax

SELECT col1,col2 FROM tablename 
WHERE col1 = value;

Example: (Table)

    Id	Full_name
    3	Anny sharma
    4	Ayesha jain
    5	Preeti jain
    6	Rahul jain

Query:

mysql> SELECT * FROM emp WHERE id = 4;

Output

    Id	Full_name
    4	Ayesha jain



HAVING Clause

  • HAVING Clauses can also be used for conditional
  • It is basically to fetch a conditional record
  • If we are using HAVING Clauses then HAVING Clauses should be mention after HAVING Clauses
  • HAVING Clauses should be implemented on grouping
  • We should implement HHAVING Clauses whenever condition required

Syntax

SELECT col1,col2, ..., coln FROM tablename 
WHERE colname = value 
GROUP BY colname HAVING colname = value;

Example: (Table)

    Id	Full_name
    3	Anny sharma
    4	Preeti jain
    5	Rahul jain
    7	Anny sharma

Query:

mysql> SELECT * FROM emp 
GROUP BY id HAVING full_name = 'Anny sharma';

Output

    Id	Full_name
    3	Anny sharma
    7	Anny sharma






You may also be interested in...

C/C++ Tips and Tricks...

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.


Comments and Discussions