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





Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.




Comments and Discussions





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.




close Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.