Sql Tutorial Class


How to use not in sql server

NOT IN keyword in SQL Query: NOT IN keyword ignore the selected value(s) and display records.


IN Keyword in SQL Server:
Syntax:
         Select [*|COLUMN_NAME,...] From [TABLE_NAME] Where NOT IN('VALUE','VALUE2',...)

Description:
             Select: SQL Keyword.
             * | COLUMN_NMME: All or specific column name.
             From: SQL Keyword.
             TABLE_NAME: Name of table.
             Where: SQL keyword.
            [NOT IN]: SQL Keyword.


Here is a Table ItemDetails
ItemCodeItemNameManufactureCompanyItemSerialNoItemCategoryItemQuantityItemPriceProductCountry
1Samsung MobileSamsungS12345Mobile500240Korea
2Samsung ProSamsungSP1234Mobile100300Korea
3Samsung HeadphoneSamsungSH2345Headphone5050Korea
4iPhoneiPoneiP12345Mobile1000500United Stat
5Apple MobileAppleA12345Mobile200300United Stat
6Apple ComputerAppleA12343Computer200400United Stat
7Apple MacbookAppleAM2345Macbook50600United Stat
8Dell ComputerDELLD02345Computer200240United Stat
9Dell LaptopDELLDL2345Laptop30240United Stat
10Apple LaptopAppleAL2345Laptop500100United Stat
11Apple HeadphoneAppleAH2345Headphone5020United Stat
12Sony CameraSonySN2345Camera200240Japan
13Sony HeadphoneSonySNH345Headphone50050Japan
14Ascer ComputerAscerAC2345Computer100600China
16Borother PrinterBrotherB12345Printer200200China
17Ascer PrinterAscerAP2345Printer200150China
18Brother PhotocopyBrotherBP2345Printer20150China
19Brother Multi PrinterBrotherBM243PhotocopyNULLNULLIndia



Now
Select ItemCode,ItemName,ManufactureCompany,ItemPrice from ItemDetails Where ManufactureCompany NOT IN('Samsung','Apple','DELL')
Result:
ItemCode ItemName ManufactureCompany ItemPrice
4 iPhone iPone 500
12 Sony Camera Sony 240
13 Sony Headphone Sony 50
14 Ascer Computer Ascer 600
16 Borother Printer Brother 200
17 Ascer Printer Ascer 150
18 Brother Photocopy Brother 150
19 Brother Multi Printer Brother NULL




Select ItemCode,ItemName,ItemCategory,ProductCountry from ItemDetails Where ItemCategory NOT IN('Mobile','iPhone')

Result:


ItemCode ItemName ItemCategory ProductCountry
3 Samsung Headphone Headphone Korea
6 Apple Computer Computer United Stat
7 Apple Macbook Macbook United Stat
8 Dell Computer Computer United Stat
9 Dell Laptop Laptop United Stat
10 Apple Laptop Laptop United Stat
11 Apple Headphone Headphone United Stat
12 Sony Camera Camera Japan
13 Sony Headphone Headphone Japan
14 Ascer Computer Computer China
16 Borother Printer Printer China
17 Ascer Printer Printer China
18 Brother Photocopy Printer China
19 Brother Multi Printer Photocopy India



Select ItemCode,ItemName,ItemPrice,ProductCountry from ItemDetails Where ItemPrice NOT IN('500','600')
Result:
ItemCode ItemName ItemPrice ProductCountry
1 Samsung Mobile 240 Korea
2 Samsung Pro 300 Korea
3 Samsung Headphone 50 Korea
5 Apple Mobile 300 United Stat
6 Apple Computer 400 United Stat
8 Dell Computer 240 United Stat
9 Dell Laptop 240 United Stat
10 Apple Laptop 100 United Stat
11 Apple Headphone 20 United Stat
12 Sony Camera 240 Japan
13 Sony Headphone 50 Japan
16 Borother Printer 200 China
17 Ascer Printer 150 China
18 Brother Photocopy 150 China




SQL NOT IN Keyword Example in Video






SQL NOT IN Keyword Example in Image


How to use not in keyword in sql server







No comments:

Post a Comment