subquery in count sql code example

Example 1: count subquery sql

/*count sub query
Depends on the use case, here are a few options*/

Select Count(*) from 
(
--Sub query
Select id from users 
) as userCount

Select * from 
(
--Sub query
select count(id) as cnt from Users where LastPasswordChangedDate < '2021-01-01'
) as LastPasswordChangedDate

Select 
(
	--Sub query
	select count(id) as cnt from Users where LastPasswordChangedDate < '2021-01-01'
) as LastPasswordChangedDateCount
, *
from Users
where LastPasswordChangedDate < '2021-01-01'

Example 2: get substract count sql

select  Count(City) - Count(Distinct(City)) from Station;

Tags:

Sql Example