Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

query to get the difference between total number of rows and distinct rows

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; 
like image 295
prabhakar Reddy G Avatar asked Oct 25 '15 06:10

prabhakar Reddy G


People also ask

What is the difference between total entries and distinct entries in SQL?

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.

How do you use COUNT and distinct in the same query?

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.

What is the difference between COUNT distinct and distinct COUNT?

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.

What is the difference between COUNT COUNT distinct and COUNT (*) in SQL?

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.


1 Answers

You can use the select distinct inside the count and try this way

select  (count(CITY)- count(distinct CITY)) from STATION; 
like image 82
ScaisEdge Avatar answered Oct 29 '22 21:10

ScaisEdge