﻿ SQL - View to count many customers at each lavel of grade

# SQL Exercises: View to count many customers at each lavel of grade

## SQL VIEW: Exercise-4 with Solution

4. From the following table, create a view that counts the number of customers in each grade.

Sample table: customer

Sample Solution:

``````-- Creating a VIEW named 'gradecount' with columns 'grade' and 'number'

-- Selecting columns 'grade' and the count of rows as 'number' from the 'customer' table
-- Grouping the result by the 'grade' column
FROM customer
``````

output:

```sqlpractice=# SELECT *
sqlpractice-# WHERE number = 2;
-------+--------
|      2
200 |      2
300 |      2
(3 rows)
```

Code Explanation:

The SQL statement creates a view called "gradecount" that shows the count of customers for each grade.
The specified two columns in the view are "grade" and "number".
It selects the "grade" column and the count of rows for each grade from the "customer" table and groups the results by grade.

Inventory database model: