← Back to MySQL Course | Chapter 5: Inserting & Selecting Data | Lesson 5 of 6

SELECT with Aliases

एक alias किसी column या table के लिए एक nickname जैसा है जो सिर्फ एक question के लिए रहता है, answer को पढ़ना आसान बनाते हुए।
Syntax
sql
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?

sql
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

sql
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

sql
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

sql
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

sql
SELECT name full_name FROM users;
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. WHERE clause में एक alias इस्तेमाल करना, जैसे WHERE total > 100, जो fail होता है क्योंकि उस stage पर aliases अभी define ही नहीं हुए होते।
  2. बिना quotes के एक multi-word alias इस्तेमाल करना, जैसे AS full name, जो एक syntax error है।
  3. यह भूल जाना कि एक alias सिर्फ उस query में exist करता है और table में column का नाम नहीं बदलता।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.