0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【SQL】DISTINCTはCOUNT関数の中にぶちこむことができる|2356. Number of Unique Subjects Taught by Each Teacher【LeetCode】

Last updated at Posted at 2025-12-12

SQL

SELECT teacher_id, COUNT(DISTINCT subject_id) AS cnt 
FROM Teacher
GROUP BY teacher_id;
  • 表題の通りで、COUNT関数を用いてカウントをする際に重複値をはじいて集計したい際はDISTINCTCOUNT(DISTINCT COLUMN_NAME)と記載することで実現可能。

詰まったところ

  • DISTINCTSELECTの後置クエリとしてか使えないイメージがあった。
    • SELECT DISTINCT的な使い方をするイメージだった。

謝辞

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?