SQL groupBy, exists, having, any & case

  1. GROUP BY Clause: The GROUP BY clause in SQL is used to group rows with similar values into summary rows, based on the values in one or more columns.

Syntax:

Example:

This query returns the average price of cars for each manufacturer, by grouping the cars by manufacturer.

  1. EXISTS Clause: The EXISTS clause in SQL is used to check if a subquery returns any data, and it returns TRUE or FALSE based on the result.

Syntax:

Example:

This query returns the model and color of all cars that have an owner.

  1. HAVING Clause: The HAVING clause in SQL is used to filter the grouped result set, based on a condition applied to the aggregate functions.

Syntax:

Example:

This query returns the average price of cars for each manufacturer, where the average price is greater than 20000.

  1. ANY Clause: The ANY clause in SQL is used with the comparison operators to match any value returned by a subquery.

Syntax:

Example:

This query returns the model and color of cars whose price is greater than the price of any Toyota car.

  1. CASE Clause: The CASE clause in SQL is used to perform conditional logic in a SELECT statement, by evaluating a set of conditions and returning a result based on the first condition that is met.

Syntax:

Example:

This query returns the model, color, and price category (Cheap, Moderate, or Expensive) of each car in the cars table

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top