Data filtering: Distinct, where, between, in, like, Union, Intersect, Except, Limit, IS NULL

SQLite का filtering toolbox result sets को narrow करता है: WHERE rows test करता है, DISTINCT duplicates हटाता है, BETWEEN/IN/LIKE/IS NULL conditions को refine करते हैं, LIMIT output को cap करता है, और UNION/INTERSECT/EXCEPT पूरी queries combine करते हैं।

11 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

एक हज़ार rows, एक सवाल

marks table अब पूरी college रखता है: हज़ारों rows। कोई भी कभी सब कुछ नहीं चाहता।

Principal को 60 से 80 के बीच DBMS scores चाहिए। Office को चाहिए students किस city से आते हैं (हर city एक बार)। Notice board को top 5 चाहिए। Exam cell को वे students चाहिए जिन्होंने एक paper miss किया (score missing)।

SQL हर एक को एक छोटे filtering keyword से जवाब देता है, और यह lesson पूरा toolbox है: दस keywords, प्रति एक काम, एक table पर जिसे आप पहले से जानते हैं।

Theory

एक pipeline पर sieves

Query को table से आपकी screen तक एक pipeline समझिए, और हर keyword को एक sieve जो आप clip करते हैं:

WHERE matching rows रखता है, DISTINCT duplicates पिघला देता है, LIMIT n rows के बाद stream काट देता है। और जब आपके पास DO pipelines हों, UNION उन्हें साथ डालता है, INTERSECT सिर्फ़ वही रखता है जो दोनों ढोते हैं, EXCEPT एक को दूसरे से subtract करता है। कोई भी combination, वही table।

At a glance

Row-filter family (सब WHERE के अंदर रहते हैं, DISTINCT/LIMIT को छोड़कर)

ToolRows रखता है जहाँ...Example
WHEREcondition true हैWHERE subject = 'DBMS'
BETWEENvalue एक range में है, ends INCLUDEDscore BETWEEN 60 AND 80
INvalue एक list में हैcity IN ('Surat', 'Navsari')
LIKEtext एक pattern से match करता है (% कोई, _ एक)name LIKE 'R%'
IS NULLvalue missing हैscore IS NULL
DISTINCT(output) duplicates हटाए गएSELECT DISTINCT city
LIMIT(output) सिर्फ़ पहली n rowsLIMIT 5

Practical

Office के चार सवाल, जवाब दिए गए

-- 1. DBMS scores from 60 to 80 (BOTH ends included)
SELECT roll, score FROM marks
WHERE subject = 'DBMS' AND score BETWEEN 60 AND 80;

-- 2. every city once
SELECT DISTINCT city FROM students;

-- 3. top 5 DBMS scores
SELECT roll, score FROM marks
WHERE subject = 'DBMS'
ORDER BY score DESC
LIMIT 5;

-- 4. students who missed the paper (score missing)
SELECT roll FROM marks
WHERE subject = 'DBMS' AND score IS NULL;

-- names starting with R:  'R%'   second letter i: '_i%'
SELECT name FROM students WHERE name LIKE 'R%';

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Theory

पूरी queries combine करना: UNION, INTERSECT, EXCEPT

Set operators दो complete SELECTs पर काम करते हैं वही columns की संख्या के साथ:

  • UNION: rows जो किसी में दिखती हैं (duplicates हटाए गए; UNION ALL उन्हें रखता है)।
  • INTERSECT: rows जो दोनों में दिखती हैं।
  • EXCEPT: rows जो पहले में हैं पर दूसरे में नहीं।

Example: जिन rolls ने DBMS pass किया INTERSECT जिन rolls ने Maths pass किया वह students देता है जिन्होंने दोनों pass किए। INTERSECT को EXCEPT से बदलिए और आपको वे मिलते हैं जिन्होंने DBMS pass किया पर Maths नहीं: re-exam list।

Quiz

एक student absentees ढूँढने के लिए लिखता है: SELECT roll FROM marks WHERE score = NULL;। यह कुछ भी return नहीं करता, हालाँकि NULL scores मौजूद हैं। क्यों?

  1. NULL unknown है, तो score = NULL कभी true नहीं है; सही test score IS NULL है
  2. Query को roll से पहले DISTINCT चाहिए
  3. NULL rows सिर्फ़ LIKE 'NULL' से मिल सकती हैं
  4. SQLite NULL को 0 के रूप में store करता है, तो test score = 0 होना चाहिए
Show the answer

NULL unknown है, तो score = NULL कभी true नहीं है; सही test score IS NULL है

NULL का मतलब है unknown, और unknown के साथ किसी भी चीज़ की comparison unknown देती है, कभी true नहीं, तो = NULL कोई rows match नहीं करता: सबसे classic SQL trap। एकमात्र सही tests IS NULL और IS NOT NULL हैं। Option D ख़तरनाक है: NULL zero NAHI है (एक 0 score एक असली score है); options B और C query को decorate करते हैं बिना असली problem को छुए।

Think first

Row counts अंदाज़ा लगाइए

marks में DBMS scores 55, 60, 80, 95 हैं (चार rows)। tap करने से पहले, हर एक कितनी rows return करता है गिनिए:

1. WHERE score BETWEEN 60 AND 80

2. WHERE score IN (55, 95)

3. WHERE score > 60 LIMIT 1

Show the answer

1. 2 rows (60 और 80: BETWEEN दोनों endpoints include करता है, वह fact जो exams test करते हैं)।

2. 2 rows (exact list membership: 55 और 95)।

3. 1 row (80 और 95 qualify करते हैं, LIMIT सिर्फ़ पहली deliver की गई रखता है)।

अगर आपने BETWEEN के लिए 1 row कहा, आपने endpoints exclude कर दिए: BETWEEN 60 AND 80 का मतलब है >= 60 AND <= 80, हमेशा inclusive।

Watch out

तीन habitual mark-losers

IS NULL के बजाय = NULL: चुपचाप कुछ नहीं return करता।

BETWEEN endpoints: inclusive, दोनों ends। अपने answer में इसके बगल में ">= and <=" लिखिए यह दिखाने के लिए कि आप जानते हैं।

LIKE anchors: 'R%' = R से शुरू; '%R' = R पर ख़त्म; '%R%' = R contain करता है; '_i%' = दूसरा letter i। Pattern को position by position पढ़िए और जब एक query explain करने को कहा जाए तो बताइए % और _ हर एक का क्या मतलब है।

Theory

Basics आप BCA105 में मिले

WHERE और DISTINCT BCA105 में Meera की shop पर दिखे; यहाँ नया क्या है वह है एक जगह पूरा toolbox plus set operators। Unit 3 में यही exact queries cursor.execute() के लिए Python strings में travel करती हैं, और Unit 4 में pandas वही idea boolean masks के रूप में फिर implement करता है (df[df.score > 60])। Filtering एक concept है जो इस subject भर तीन costumes पहनता है।

Summary

Key takeaways

  • WHERE हर row test करता है; BETWEEN (inclusive), IN (list), LIKE (% कोई run, _ एक char), IS NULL इसे refine करते हैं।
  • = NULL कभी match नहीं करता: IS NULL / IS NOT NULL एकमात्र सही null tests हैं।
  • DISTINCT duplicate output rows हटाता है; LIMIT n output cap करता है (top-n के लिए ORDER BY के साथ जोड़िए)।
  • UNION (किसी में, कोई duplicates नहीं), INTERSECT (दोनों में), EXCEPT (पहला minus दूसरा) दो समान-width SELECTs combine करते हैं।
  • UNION ALL duplicates रखता है।
  • Memory hook: एक pipeline पर clip किए sieves।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to SQLite

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati