SQL SELECT Statement

SQL SELECT Statement क्या है

SQL SELECT Statement का उपयोग database table से data retrieve करने के लिए किया जाता है। यह SQL का सबसे ज्यादा इस्तेमाल होने वाला statement है। SELECT statement की मदद से आप पूरी table, specific columns या condition-based data fetch कर सकते हैं।

Basic SELECT Syntax

SELECT column1, column2
FROM table_name;

अगर table के सभी columns का data चाहिए, तो * wildcard का उपयोग किया जाता है।

SELECT * FROM table_name;

SELECT Statement का Example

मान लीजिए हमारे पास students नाम की table है:

idnameagecity
1Rahul20Delhi
2Aman22Jaipur
3Neha21Mumbai

Example 1: सभी records fetch करना

SELECT * FROM students;

यह query students table से सभी rows और columns return करेगी।

Example 2: Specific columns fetch करना

SELECT name, age
FROM students;

यह query सिर्फ name और age columns का data return करेगी।

SELECT DISTINCT

SELECT DISTINCT का उपयोग duplicate values हटाने के लिए किया जाता है।

Example:

SELECT DISTINCT city
FROM students;

यह query unique cities की list return करेगी।

SELECT with WHERE Clause

WHERE clause का उपयोग condition लगाने के लिए किया जाता है।

Example:

SELECT * FROM students
WHERE city = 'Delhi';

यह query सिर्फ उन्हीं students को दिखाएगी जो Delhi में रहते हैं।

Multiple conditions का example:

SELECT name, age
FROM students
WHERE age > 20 AND city = 'Jaipur';

SELECT with ORDER BY

ORDER BY result को ascending या descending order में sort करता है।

Default order ascending होता है।

SELECT * FROM students
ORDER BY age;

Descending order:

SELECT * FROM students
ORDER BY age DESC;

SELECT with LIMIT

LIMIT का उपयोग result की number of rows control करने के लिए किया जाता है।

SELECT * FROM students
LIMIT 2;

यह query सिर्फ पहली 2 rows return करेगी।

SELECT with Aliases

Alias का उपयोग column या table को temporary नाम देने के लिए किया जाता है।

SELECT name AS student_name, age AS student_age
FROM students;

SELECT with Expressions

SELECT statement में expressions का भी उपयोग किया जा सकता है।

SELECT name, age + 1 AS next_year_age
FROM students;

SELECT with Functions

SELECT के साथ built-in functions का उपयोग किया जा सकता है।

SELECT COUNT(*) AS total_students
FROM students;

SELECT without FROM

कुछ databases में SELECT statement बिना FROM के भी इस्तेमाल किया जा सकता है।

SELECT 5 + 10;

Common Mistakes in SELECT Statement

  • Column name गलत लिखना
  • WHERE clause में quotes miss करना
  • ORDER BY में wrong column use करना

Incorrect example:

SELECT name age FROM students;

Correct example:

SELECT name, age FROM students;

Summary

  • SELECT statement data retrieve करने के लिए use होता है
  • * सभी columns select करता है
  • WHERE, ORDER BY, LIMIT के साथ SELECT powerful बन जाता है
  • SELECT statement SQL की foundation है
Share your love