Filtering rows

In SQL, the WHERE keyword allows you to filter based on both text and numeric values in a table. There are a few different comparison operators you can use:

=equal

<>not equal

>greater than

<=less than or equal to

>=greater than or equal to

For example, you can filter text records such astitle. The following code returns all films with the title'Metropolis':

SELECT title

FROM films

WHERE title = 'Metropolis';

Notice that the WHERE clause always comes after the FROM statement!

Note that in this course we will use<>and not!=for the not equal operator, as per the SQL standard.

Remember, the WHERE clause can also be used to filter text results, such as names or countries.

For example, this query gets the titles of all films which were filmed in China:

SELECT title

FROM films

WHERE country = 'China';

Often, you'll want to select data based on multiple conditions. You can build up your WHERE queries by combining multiple conditions with the AND keyword.

For example,

SELECT title

FROM films

WHERE release_year > 1994

AND release_year < 2000;

What if you want to select rows based on multiple conditions where some but not?all?of the conditions need to be met? For this, SQL has the OR operator.

For example, the following returns all films released in?either?1994 or 2000:

SELECT title

FROM films

WHERE release_year = 1994

OR release_year = 2000;

Checking for ranges like this is very common, so in SQL the BETWEEN keyword provides a useful shorthand for filtering values within a specified range. This query is equivalent to the one above:

SELECT title

FROM films

WHERE release_year

BETWEEN 1994 AND 2000;

Enter the IN operator! The IN operator allows you to specify multiple values in a WHERE clause, making it easier and quicker to specify multiple OR conditions! Neat, right?

So, the above example would become simply:

SELECT name

FROM kids

WHERE age IN (2, 4, 6, 8, 10);

In SQL,?NULL represents a missing or unknown value. You can check for NULL values using the expression IS NULL. For example, to count the number of missing birth dates in the people table:

SELECT COUNT(*)

FROM people

WHERE birthdate IS NULL;

As you've seen, the WHERE clause can be used to filter text data. However, so far you've only been able to filter by specifying the exact text you're interested in. In the real world, often you'll want to search for apatternrather than a specific text string.

In SQL, the LIKE operator can be used in a WHERE clause to search for a pattern in a column. To accomplish this, you use something called a?wildcard?as a placeholder for some other values. There are two wildcards you can use with LIKE:

The % wildcard will match zero, one, or many characters in text. For example, the following query matches companies like'Data','DataC''DataCamp','DataMind', and so on:

SELECT name

FROM companies

WHERE name LIKE 'Data%';

The_wildcard will match asinglecharacter. For example, the following query matches companies like'DataCamp','DataComp', and so on:

SELECT name

FROM companies

WHERE name LIKE 'DataC_mp';

You can also use theNOT LIKEoperator to find records thatdon'tmatch the pattern you specify.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末误甚,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子丘薛,更是在濱河造成了極大的恐慌缚去,老刑警劉巖残腌,帶你破解...
    沈念sama閱讀 212,718評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件智政,死亡現(xiàn)場(chǎng)離奇詭異允瞧,居然都是意外死亡经磅,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,683評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門(mén)澎胡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)乍炉,“玉大人,你說(shuō)我怎么就攤上這事滤馍。” “怎么了底循?”我有些...
    開(kāi)封第一講書(shū)人閱讀 158,207評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵巢株,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我熙涤,道長(zhǎng)阁苞,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,755評(píng)論 1 284
  • 正文 為了忘掉前任祠挫,我火速辦了婚禮那槽,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘等舔。我一直安慰自己骚灸,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,862評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布慌植。 她就那樣靜靜地躺著甚牲,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蝶柿。 梳的紋絲不亂的頭發(fā)上丈钙,一...
    開(kāi)封第一講書(shū)人閱讀 50,050評(píng)論 1 291
  • 那天,我揣著相機(jī)與錄音交汤,去河邊找鬼雏赦。 笑死,一個(gè)胖子當(dāng)著我的面吹牛芙扎,可吹牛的內(nèi)容都是我干的星岗。 我是一名探鬼主播,決...
    沈念sama閱讀 39,136評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼戒洼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼伍茄!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起施逾,我...
    開(kāi)封第一講書(shū)人閱讀 37,882評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤敷矫,失蹤者是張志新(化名)和其女友劉穎例获,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體曹仗,經(jīng)...
    沈念sama閱讀 44,330評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡榨汤,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,651評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了怎茫。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片收壕。...
    茶點(diǎn)故事閱讀 38,789評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖轨蛤,靈堂內(nèi)的尸體忽然破棺而出蜜宪,到底是詐尸還是另有隱情,我是刑警寧澤祥山,帶...
    沈念sama閱讀 34,477評(píng)論 4 333
  • 正文 年R本政府宣布圃验,位于F島的核電站,受9級(jí)特大地震影響缝呕,放射性物質(zhì)發(fā)生泄漏澳窑。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,135評(píng)論 3 317
  • 文/蒙蒙 一供常、第九天 我趴在偏房一處隱蔽的房頂上張望摊聋。 院中可真熱鬧,春花似錦栈暇、人聲如沸麻裁。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,864評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)悲立。三九已至,卻和暖如春新博,著一層夾襖步出監(jiān)牢的瞬間薪夕,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,099評(píng)論 1 267
  • 我被黑心中介騙來(lái)泰國(guó)打工赫悄, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留原献,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,598評(píng)論 2 362
  • 正文 我出身青樓埂淮,卻偏偏與公主長(zhǎng)得像姑隅,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子倔撞,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,697評(píng)論 2 351

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,442評(píng)論 0 23
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,447評(píng)論 5 6
  • 我很早就對(duì)自己的職業(yè)進(jìn)行過(guò)設(shè)想讲仰,在大學(xué)期間曾經(jīng)為自己未來(lái)的職業(yè)選擇頭痛過(guò)整整一個(gè)月,最后確定自己想干什么痪蝇。其實(shí)鄙陡,還...
    莉絲lxb閱讀 220評(píng)論 0 0
  • 在六月里冕房,仿佛有一半的日子都在飄著大雨。每次路過(guò)樓下那棵樹(shù)時(shí)趁矾,都想停留幾秒鐘看看地上飄落的葉子和花朵耙册。白色的花,沾...
    維納斯和米米閱讀 402評(píng)論 0 0