MulaiMulai sekarang secara gratis

Remove duplicates with DISTINCT()

You want to know the closest city to earthquakes with a magnitude of 8 or higher. You can get this information from the Earthquakes table. However, a simple query returns duplicate rows because some cities have experienced more than one magnitude 8 or higher earthquake.

You can remove duplicates by using the DISTINCT() clause. Once you have your results, you would like to know how many times each city has experienced an earthquake of magnitude 8 or higher.

Note that IS NOT NULL is being used because many earthquakes do not occur near any populated area, thankfully.

Latihan ini adalah bagian dari kursus

Improving Query Performance in SQL Server

Lihat Kursus

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

SELECT ___, -- Add the closest city
		Country 
FROM Earthquakes
WHERE Magnitude >= 8
	AND NearestPop IS NOT NULL
ORDER BY NearestPop;
Edit dan Jalankan Kode