COUNT

The COUNT() function returns the count of rows of data that satisfy the conditions specified in the query. They can be used to count the full contents of the table or specific rows that satisfy conditions in the query. Let's look at some examples.

Lets begin by selecting all data for product_name, supplier_id and cost

SELECT 
    product_name,
    supplier_id,
    cost 
FROM products
     product_name       | supplier_id |  cost
------------------------+-------------+---------
Samsung Galaxy S23      |           2 |  520.00
MacBook Pro 16"         |           1 | 1800.00
Dell XPS 13             |           3 |  900.00
iPad Air                |           1 |  400.00
AirPods Pro             |           1 |  150.00
Sony WH-1000XM4         |           4 |  200.00
Nintendo Switch         |           5 |  180.00
PlayStation 5           |           5 |  350.00
Xbox Series X           |           6 |  350.00
Apple Watch Series 8    |           1 |  250.00
Samsung 65" QLED TV     |           2 | 1200.00
LG OLED 55"             |           7 |  900.00
Canon EOS R5            |           8 | 2800.00
GoPro Hero 11           |           9 |  250.00
DJI Mini 3              |          10 |  450.00
Logitech MX Master 3    |          11 |   60.00
Mechanical Keyboard RGB |          12 |   80.00
Monitor 27" 4K          |          13 |  280.00
Webcam HD 1080p         |          14 |   40.00
USB-C Hub               |          15 |   25.00
Wireless Charger        |          16 |   20.00
Bluetooth Speaker       |          17 |   70.00
Smart Home Hub          |          18 |  120.00
Security Camera         |          19 |   90.00
Smart Doorbell          |          20 |  150.00
Fitness Tracker         |          21 |  120.00
VR Headset              |          22 |  250.00
Robot Vacuum            |          23 |  350.00
Air Fryer               |          24 |   70.00
Coffee Maker            |          25 |  110.00
Electric Toothbrush     |          26 |   45.00
Hair Dryer Pro          |          27 |   80.00
Massage Gun             |          28 |  120.00
Power Bank 20000mAh     |          29 |   30.00
Car Phone Mount         |          30 |   15.00
Dash Cam                |          31 |   65.00
LED Strip Lights        |          32 |   25.00
Smart Thermostat        |          33 |  180.00
Outdoor Security Light  |          34 |   50.00
Gaming Mouse RGB        |          35 |   40.00
Gaming Headset          |          36 |   85.00
External SSD 1TB        |          37 |   85.00
USB Flash Drive 128GB   |          38 |   12.00
Laptop Stand            |          39 |   35.00
Document Camera         |          40 |  120.00
Label Maker             |          41 |   40.00
Paper Shredder          |          42 |   85.00
Projector Mini          |          43 |  180.00
Smart Scale             |          44 |   45.00
Electric Kettle         |          45 |   25.00
Tablet Stand            |          46 |   18.00
iPhone 14 Pro           |           1 |  650.00
(52 rows)

Let's count the number of distinct product_names in the products table. The query will look like this:

SELECT COUNT(distinct product_name)
FROM products;
 count
-------
    52
(1 row)

Let's query the products table to count allsupplier_id whose cost is over 100.

select count(distinct supplier_id) as suppliers
from products
where cost > 100
 count
-------
    21
(1 row)