# SELECT, WHERE & ORDER BY

Code With Purpose · Hinglish · Free starter lesson

## Concept

Relational table mein rows aur named columns hote hain. SELECT batata hai kaunse columns chahiye; FROM batata hai table ka naam. WHERE individual rows ko condition se filter karta hai, jaise score >= 70. ORDER BY result ko sort karta hai; DESC ka matlab descending hai.

Text literals ke liye single quotes use karo. AND mein dono conditions true honi chahiye; OR mein kam se kam ek. NULL missing ya unknown information ko represent karta hai. Isko check karne ke liye IS NULL ya IS NOT NULL use karo, = NULL nahi.

Example SQLite aur Python ke built-in sqlite3 module ka hai, isliye alag server nahi chahiye. Database is run ke liye memory mein rahega. Dusre SQL dialects alag ho sakte hain. Real user input ke liye query parameters use karo, strings jodkar SQL mat banao.

## Example

```python
import sqlite3
db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE learners (name TEXT, score INTEGER)")
db.executemany("INSERT INTO learners VALUES (?, ?)",
               [("Asha", 82), ("Ravi", 65), ("Meera", 91)])
rows = db.execute("""
    SELECT name, score FROM learners
    WHERE score >= 70 ORDER BY score DESC
""").fetchall()
print(rows)
db.close()
```

## Expected output

```text
[('Meera', 91), ('Asha', 82)]
```

## Exercise

Isi table se un learners ke sirf names nikalo jinka score 65 se 85 tak hai, dono limits included. Name se sort karo.

## Suggested solution

```
SELECT name
FROM learners
WHERE score BETWEEN 65 AND 85
ORDER BY name;
-- Asha, Ravi
```

## Takeaway

Rows ko condition se filter karo aur ordering consciously choose karo.

Reference: https://www.sqlite.org/lang_select.html

https://codewithpurposeai.in/lesson-sql-select-filter.html?lang=hinglish
