What is the equivalent of Python Pandas value_counts in SQL?

cqcn1991 Source

I learned python and pandas before SQL, so this question is a bit basic.

For example, I have a type column with values like 1, 2, 3.

Then when I do df['type'].value_counts, I can get the statistics of the type, maybe something like

1: 1000 rows
2: 220 rows
3: 100 rows

I want to know What is the equivalent in SQL? I believe it should be something about group_by and count?

pythonsqlpandas

Answers

answered 6 months ago Turophile #1

If you want to know how many times each value occurs in a column, use:

SELECT type, count(*)
FROM table
GROUP BY type

answered 6 months ago Ash Chakraborty #2

SELECT type, count(1) as num_types
FROM table
GROUP BY type

will return the equivalent row counts.

comments powered by Disqus