SQL creating view with count() function
View with count() function
In this page, we are discussing usage of aggregate COUNT() function along with the VIEW in a CREATE VIEW statement.
Example:
Sample table: customer
To create a view 'noofgrade' with two columns 'grade' and 'gradecount' of the table 'customer' with following conditions -
1. 'gradecount' column created with number of grades from the customer table,
2. unique 'grade' of 'customer' table should be grouped,
the following SQL statement can be used:
SQL Code:
CREATE VIEW noofgrade(grade,gradecount)
AS SELECT grade,COUNT(*)
FROM customer
GROUP BY grade;
Output:
To execute query on this view
SQL Code:
SELECT * FROM noofgrade;
SQL creating view with count(), sum() and avg()
In the following, to discuss the SQL VIEW we have used the aggregate function COUNT() and SUM() and AVG() with the SQL CREATE VIEW statement.
Example:
Sample table: orders
To create a view 'ordersview' from the table 'orders' with following conditions -
1. 'c_cust_code' column must be created with COUNT(DISTINCT cust_code) from the 'orders' table,
2. 'c_ag_code' column must be created with COUNT(DISTINCT agent_code) from the 'orders' table,
3. 'c_ord_num' column must be created with COUNT(ord_num) from the 'orders' table,
4. 'avg_ord_amt' column must be created with AVG(ord_amount) from the 'orders' table,
5. 'sum_amt' column must be created with SUM(ord_amount) from the 'orders' table,
6. unique 'ord_date' must be within the group,
the following SQL statement can be used:
SQL Code:
CREATE VIEW ordersview (ord_date,c_cust_code,
c_ag_code,c_ord_num,avg_ord_amt,sum_amt)
AS SELECT ord_date,COUNT(DISTINCT cust_code),
COUNT(DISTINCT agent_code),COUNT(ord_num),
AVG(ord_amount), SUM(ord_amount)
FROM orders
GROUP BY ord_date;
Output:
To execute query on this view
sql Code:
SELECT * FROM ordersview;
Practice SQL Exercises
- SQL Exercises, Practice, Solution
- SQL Retrieve data from tables [33 Exercises]
- SQL Boolean and Relational operators [12 Exercises]
- SQL Wildcard and Special operators [22 Exercises]
- SQL Aggregate Functions [25 Exercises]
- SQL Formatting query output [10 Exercises]
- SQL Quering on Multiple Tables [8 Exercises]
- FILTERING and SORTING on HR Database [38 Exercises]
- SQL JOINS
- SQL SUBQUERIES
- SQL Union[9 Exercises]
- SQL View[16 Exercises]
- SQL User Account Management [16 Exercise]
- Movie Database
- BASIC queries on movie Database [10 Exercises]
- SUBQUERIES on movie Database [16 Exercises]
- JOINS on movie Database [24 Exercises]
- Soccer Database
- Introduction
- BASIC queries on soccer Database [29 Exercises]
- SUBQUERIES on soccer Database [33 Exercises]
- Hospital Database
- Employee Database
- More to come!
Want to improve the above article? Contribute your Notes/Comments/Examples through Disqus.
Previous: Create View
Next: Create view with join
SQL: Tips of the Day
SQL Server SELECT into existing table.
INSERT INTO dbo.TABLETWO SELECT col1, col2 FROM dbo.TABLEONE WHERE col3 LIKE @search_key
This assumes there's only two columns in dbo.TABLETWO - you need to specify the columns otherwise:
INSERT INTO dbo.TABLETWO (col1, col2) SELECT col1, col2 FROM dbo.TABLEONE WHERE col3 LIKE @search_key
Database: SQL Server
Ref: https://bit.ly/3y6tpA3
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework