SELECT with Aliases
In this page:
SELECT column_name AS alias_name FROM table_name;
SELECT expression AS alias_name FROM table_name;
एक Alias क्या है?
एक alias एक temporary, query-scoped name है जो आप AS इस्तेमाल करके किसी column या table को assign करते हैं, जो result sets और complex queries को पढ़ना और reference करना कहीं ज़्यादा आसान बनाता है। Aliases सिर्फ उस single query की अवधि तक exist करते हैं और schema में actual table या column names पर कोई effect नहीं रखते।
उदाहरण: What is an Alias?
SELECT name AS full_name FROM users;
Calculations के साथ Aliases
Aliases calculated columns पर खासतौर पर valuable हो जाते हैं — बिना एक के, एक computed expression results में total_price जैसे meaningful नाम के बजाय एक unreadable auto-generated name के तहत दिखता है।
उदाहरण: Aliases with Calculations
SELECT price * quantity AS total FROM orders;
Multi-Word Aliases
अगर एक alias में एक space या दूसरा special character हो, इसे quotes में wrap करना ज़रूरी है ताकि MySQL इसे कई अलग tokens के बजाय एक single name की तरह parse करे। किसी multi-word alias के आसपास quotes भूल जाना एक common syntax error है जो इसके बजाय एक confusing 'unknown column' message produce करता है।
उदाहरण: Multi-Word Aliases
SELECT price * quantity AS 'Order Total' FROM orders;
Table Aliases
किसी table को एक short alias देना आपको एक लंबी query भर में कहीं कम typing के साथ इसे reference करने देता है, जो एक बार आप एक statement में कई tables साथ join कर रहे हों तो बहुत मायने रखता है। यह तब essential हो जाता है जब एक query same table को खुद से join करे, क्योंकि MySQL को दो references को अलग करने का एक तरीका चाहिए।
उदाहरण: Table Aliases
SELECT u.name FROM users AS u;
AS Keyword Omit करना
AS keyword MySQL में technically optional है — expression और उसके alias के बीच बस एक space लिखना identically काम करता है, हालाँकि कई style guides clarity के लिए AS रखने की recommend करती हैं।
उदाहरण: Omitting the AS Keyword
SELECT name full_name FROM users;
WHEREclause में एक alias इस्तेमाल करना, जैसेWHERE total > 100, जो fail होता है क्योंकि उस stage पर aliases अभी define ही नहीं हुए होते।- बिना quotes के एक multi-word alias इस्तेमाल करना, जैसे
AS full name, जो एक syntax error है। - यह भूल जाना कि एक alias सिर्फ उस query में exist करता है और table में column का नाम नहीं बदलता।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: