I'm new to SQL, wasn't able to write the correct SQL. Given a table STATION that holds data for five fields namely ID, CITY, STATE, NORTHERN LATITUDE and WESTERN LONGITUDE.
+-------------+------------+
| Field | Type |
+-------------+------------+
| ID | INTEGER |
| CITY | VARCHAR(21)|
| STATE | VARCHAR(2) |
| LAT_N | NUMERIC |
| LONG_W | NUMERIC |
+-------------+------------+
Let NUM
be the number of cities and NUMunique
be the number of unique cities, then write a query to print the value of NUM
- NUMunique
.
I tried:
select (count(CITY)- distinct count(CITY)) from STATION;
The query that shows the difference between the total number of city entries in the table and the number of distinct city entries in the table is following. SELECT COUNT (CITY) – COUNT (DISTINCT CITY) FROM STATION; Here STATION is the table name and by using SELECT all the possible entries can be displayed.
Yes, you can use COUNT() and DISTINCT together to display the count of only distinct rows. SELECT COUNT(DISTINCT yourColumnName) AS anyVariableName FROM yourTableName; To understand the above syntax, let us create a table. Display all records from the table using select statement.
The COUNT DISTINCT function returns the number of unique values in the column or expression, as the following example shows. SELECT COUNT (DISTINCT item_num) FROM items; If the COUNT DISTINCT function encounters NULL values, it ignores them unless every value in the specified column is NULL.
The simple answer is no – there is no difference at all. The COUNT(*) function counts the total rows in the table, including the NULL values.
You can use the select distinct inside the count and try this way
select (count(CITY)- count(distinct CITY)) from STATION;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With