Page 271 - PYTHON-12
P. 271
For example,
mysql> SELECT * FROM student WHERE Stream IN (‘Science’, ‘Commerce’, ‘Humanities’);
Resultant table: student
Rollno Name Gender Marks DOB Mobile_no Stream
1. Raj Kumar M 93 17-Nov-2000 9586774748 Science
2. Deep Singh M 98 22-Aug-1996 8988886577 Commerce
3. Ankit Sharma M 76 02-Feb-2000 NULL Science
4. Radhika Gupta F 78 03-Dec-1999 9818675444 Humanities
6. Diksha Sharma F 80 17-Dec-1999 9897666650 Humanities
7. Gurpreet Kaur F NULL 04-Jan-2000 7560875609 Science
8. Akshay Dureja M 90 05-May-1997 9560567890 Commerce
10. Prateek Mittal M 75 25-Dec-2000 9999967543 Science
8 rows in a set (0.02 sec)
The above command displays all those records whose Stream is either Science or Commerce or
Humanities.
NOT IN
The NOT IN operator works opposite to IN operator. It matches, finds and returns the rows that do
not match the list.
For example,
mysql> SELECT * FROM student WHERE Stream NOT IN (‘Science’, ‘Commerce’, ‘Humanities’);
Resultant table: student
Rollno Name Gender Marks DOB Mobile_no Stream
5. Payal Goel F 82 21-April-1998 9845639990 Vocational
9. Shreya Anand F 70 08-Oct-1999 NULL Vocational
2 rows in a set (0.02 sec)
12.17.3 Conditions Based on Pattern—LIKE
The LIKE operator is used to search for a specified pattern in a column. This operator is used with
the columns of type CHAR. The LIKE operator searches the column to find if a part of this column
matches the string specified in the parentheses after the LIKE operator in the command.
Conditions Based on Pattern—WILD CARD CHARACTERS
The SQL LIKE condition allows you to use wild cards to perform pattern matching. SQL provides
two wild card characters that are used while comparing the strings with LIKE operator:
a. Percent(%) Matches any string
Relational Database and SQL
b. Underscore(_) Matches any one character
Syntax for LIKE:
SELECT <column_name(s)>
FROM <table_name>
WHERE <column_name> LIKE <pattern>;
12.41