Tutorials  /  Databases

SQL SELECT statement with COUNT() function

Ccentron Redaktion · January 2024 ·4 min read ·Databases, Tutorial

What is SQL SELECT statement?

SQL SELECT statement helps us select and display the data values from the particular table of the database.

DB

Matching infrastructure at centron

Databases need care in production: centron can take over updates, backups and monitoring – from a single instance to a full cluster. Explore managed clusters →

Syntax:

SQL
SELECT columns 
FROM Table-name;

Example:

SQL
SELECT * from Info;

SELECT * statement helps select all the data values from the provided table.

Output:

Code
id	Cost	city
1	100	Pune
2	100	Satara
3	65	Pune
4	97	Mumbai
5	12	USA

What is SQL COUNT() function?

SQL COUNT() function counts the total number of rows present in the database.

Syntax:

Code
COUNT(column-name)

Example:

SQL
SELECT Count(City)
from Info;

In this example, we have displayed the count of all the data rows under the column - ‘city’ of table - ‘Info’.

Output:

Code
5

Variations of SQL SELECT statement with COUNT() function

You can use the SQL SELECT statement with the COUNT() function to select and display the count of rows in a table of a database.

Along with this, we can club SQL SELECT statement with COUNT() function in various different ways.

Having understood the working of SQL SELECT COUNT(), let us now understand different variations associated with the same through examples.

Examples of SQL SELECT with COUNT() function

To display the variations in SQL SELECT COUNT(), we have used SQL CREATE query to create a Table and SQL INSERT query to input data to the database.

We will be using the below table and its data in the further examples.

SQL
create table Info(id integer, Cost integer, city varchar(200));
insert into Info(id, Cost,city) values(1, 100,"Pune");
insert into Info(id, Cost,city) values(2, 100, "Satara");
insert into Info(id, Cost,city) values(3, 65,"Pune");
insert into Info(id, Cost,city) values(4, 97,"Mumbai");
insert into Info(id, Cost,city) values(5, 12,"USA");
select * from Info;

Output:

Code
id	Cost	city
1	100	Pune
2	100	Satara
3	65	Pune
4	97	Mumbai
5	12	USA

1. SQL SELECT COUNT with WHERE clause

SQL SELECT COUNT() can be clubbed with SQL WHERE clause.

Using the WHERE clause, we have access to restrict the data to be fed to the COUNT() function and SELECT statement through a condition.

Example:

SQL
SELECT COUNT(city)
FROM Info
WHERE Cost>50;

Output:

Code
3

2. SQL SELECT COUNT(*) function

SQL SELECT statement can be used along with COUNT(*) function to count and display the data values.

The COUNT(*) function represents the count of all rows present in the table (including the NULL and NON-NULL values).

Example:

SQL
SELECT COUNT(*)
FROM Info;

Output:

Code
5

3. SQL SELECT COUNT with DISTINCT clause

The DISTINCT clause helps exclude the redundant data and displays only the unique values from the selected column.

SQL SELECT COUNT() function can be used along with DISTINCT clause to count and display the number of rows representing unique(non-repeated) values.

Example:

SQL
SELECT COUNT(DISTINCT Cost)
FROM Info;

Output:

Code
4

4. SQL SELECT COUNT with HAVING and GROUP BY clause

SQL SELECT COUNT() function can be clubbed with GROUP BY and HAVING clause to add conditions before the selection of data as well as grouping of data rows by a particular column value.

Example:

SQL
SELECT city, COUNT(Cost)
FROM Info
GROUP BY city
HAVING COUNT(Cost)>1;

Output:

Code
city	COUNT(Cost)
Pune	2

Conclusion

By this, we have come to an end of this topic. Please feel free to comment below in case you come across any doubt.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Databases
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren