Explain SQL describing COUNT aggregate and CURRENT DATE function

SQLSQLiteDatabase

Problem: Write a SQL query to count the number of orders which were placed today from the ORDERS DB2 table. (The date should not be hardcoded)

Solution

We can find the count of orders which are placed today using the below DB2 query:

Example

SELECT COUNT(ORDER_ID) AS ORDER_COUNT FROM ORDERS
WHERE ORDER_DATE = CURRENT DATE

In this query, we have used the COUNT COLUMN function which will count the total number of ORDER_ID (primary key). In the WHERE clause, we will use the predicate for the ORDER_DATE column. The CURRENT DATE is a DB2 inbuilt function which will return the current system date.

For example, if we have below ORDERS DB2 table:

ORDER_ID
ORDER_TOTAL
ORDER_DATE
Z22345
342
28-07-2020
Z62998
543
30-07-2020
Z56990
431
28-07-2020
Z56902
6743
29-07-2020
Z99781
443
27-07-2020
Z56112
889
30-07-2020

 

If the current date is 30-07-2020, then the query with CURRENT DATE predicate will return the below result:

ORDER_COUNT
2
raja
Published on 01-Dec-2020 09:07:45
Advertisements