SQL Server में क्वेरी परफ़ॉर्मेंस बेहतर करना
Dean Smith
Founder, Atamai Analytics
SELECT CountryName,
Code2,
Capital,
Pop2017
FROM Nations
INNER JOIN Cities
ON Capital = CityName;
-----------------------------------------------
-- त्रुटि, Pop2017 कॉलम
Nations और Cities दोनों tables में है
Ambiguous column name 'Pop2017'.
-- tables का alias; Nations को n और Cities को c
SELECT n.CountryName,
n.Code2,
n.Capital,
c.Pop2017 -- City population
FROM Nations AS n
INNER JOIN Cities AS c
ON n.Capital = c.CityName;
| CountryName | Code2 | Capital | Pop2017 |
|---|---|---|---|
| United Kingdom | GB | London | 346774 |
| Canada | CA | Ottawa | 874433 |
| France | FR | Paris | 10437 |
| Reunion | RE | Saint-Denis | 1067 |
| ... | ... | ... | ... |
-- columns का alias
SELECT n.CountryName AS Country,
n.Code2 AS CountryCode,
n.Capital,
c.Pop2017 AS Population
FROM Nations AS n
INNER JOIN Cities AS c
ON n.Capital = c.CityName;
| Country | CountryCode | Capital | Population |
|---|---|---|---|
| United Kingdom | GB | London | 346774 |
| Canada | CA | Ottawa | 874433 |
| France | FR | Paris | 10437 |
| Reunion | RE | Saint-Denis | 1067 |
| ... | ... | ... | ... |
-- नए column का alias MaxMagnitude
SELECT Country,
NearestPop AS City,
MAX(Magnitude) AS MaxMagnitude
FROM Earthquakes
GROUP BY Country, NearestPop;
| Country | City | MaxMagnitude |
|---|---|---|
| PE | Acar | 7.1 |
| US | Aguadilla | 7.7 |
| MX | Aguililla | 7.2 |
| PW | Airai | 7.8 |
| PG | Aitape | 7.6 |
| ... | ... | ... |
SELECT n.CountryName AS Country,
n.Capital,
e.MaxMagnitude
FROM Nations n
INNER JOIN
(SELECT Country, NearestPop AS City
,MAX(Magnitude) AS MaxMagnitude
FROM Earthquakes
GROUP BY Country, NearestPop) e
-- sub-query का alias e
ON n.Code2 = e.Country AND n.Capital = e.City;
| Country | Capital | MaxMagnitude |
|---|---|---|
| Fiji | Suva | 7.9 |
| Guam | Hagatna | 7.8 |
| Peru | Lima | 7.6 |
| Turkmenistan | Ashgabat | 7.3 |
| ... | ... | ... |
SQL Server में क्वेरी परफ़ॉर्मेंस बेहतर करना