[SQL]: Logical Operators
SQL
09/28/2019
Documentation on Comparison functions and operators
Students data available from
students.sqlstudents.txt
NOT Equal
SELECT major FROM studentsWHERE major != "Computer Science";
+--------------+| major |+--------------+| Biology || Linguistics || Chemistry || Accounting || Business || Biochemistry || Physics || Art || Biology || no_major || Japanese 10% || Food Science || Economics |+--------------+
Shows all majors except for Computer Science. Case Sensitive.
NOT LIKE
SELECT fname, age FROM studentsWHERE age NOT LIKE '2%';
+--------+------+| fname | age |+--------+------+| Ellis | 18 || John | 19 || Lamont | 18 || Diego | 17 || Mary | 16 || Lena | 31 || Ellis | 18 |+--------+------+
Show ages that are not twenties. Better practice may be using range (upcoming)
Greater & Less than
SELECT fname, credit FROM studentsWHERE credit < 50;
+----------+--------+| fname | credit |+----------+--------+| Ellis | 13 || John | 21 || Brent | 11 || Lamont | 37 || Diego | 6 || Angelina | 41 || Brad | 13 || Mary | 25 |+----------+--------+
SELECT CONCAT(fname, ' ', lname) AS fullname, ageFROM studentsWHERE age >= 25;
+------------------+------+| fullname | age |+------------------+------+| Sam Smith | 25 || Angelina Randall | 26 || Dan Joyce | 27 || Lena Shah | 31 |+------------------+------+
Shows full name of students whose age is greater than or equal to 25 inclusive
Boolean
SELECT 99 > 1;SELECT -15 > 15;
+--------+| 99 > 1 |+--------+| 1 |+--------++----------+| -15 > 15 |+----------+| 0 |+----------+
1 represents True and 0 represents false
SELECT 'a' > 'b'; -- falseSELECT 'a' < 'b'; -- trueSELECT 'A' > 'a'; -- falseSELECT 'A' >= 'a'; --String comparison is case insensitive
+-----------+| 'a' > 'b' |+-----------+| 0 |+-----------++-----------+| 'a' < 'b' |+-----------+| 1 |+-----------++-----------+| 'A' > 'a' |+-----------+| 0 |+-----------++------------+| 'A' >= 'a' |+------------+| 1 |+------------+
Avoid doing string comparison!
AND (&&)
SELECT fname, credit FROM studentsWHERE lname='Min' AND credit=13;SELECT fname, credit FROM studentsWHERE lname='Min' && credit=13;
+-------+--------+| fname | credit |+-------+--------+| Ellis | 13 |+-------+--------+
Both yields the same result
AND & LIKE
SELECT fname, major, age, credit FROM studentsWHERE age > 20 && credit < 200 &&major NOT LIKE '%science%';
Show students older than 20, has less than 200 credits, and major doesn't include "science"
OR(||)
SELECT fname, major FROM studentsWHERE major LIKE "%sCienCE" ORmajor="bioLOGY";---------SELECT fname, major FROM studentsWHERE major LIKE "%sCienCE" ||major="bioLOGY";
+--------+------------------+| fname | major |+--------+------------------+| Ellis | Computer Science || Tenzin | Biology || Sam | Computer Science || Brad | Biology || Lena | Food Science |+--------+------------------+
Again case insensitive. Both yields the same result where it shows students that study biology OR major that includes "science"
BETWEEN
SELECT fname, ageFROM studentsWHERE age >= 20 AND age < 23;---SELECT fname, age FROM studentsWHERE age BETWEEN 20 AND 22;
+----------+------+| fname | age |+----------+------+| Tenzin | 22 || Brent | 22 || Jerimiah | 20 || Brad | 22 |+----------+------+
Both yields the same result--shows students between age of 20 and 22 inclusive. BETWEEN is inclusive!
NOT BETWEEN
SELECT fname, age FROM studentsWHERE age NOT BETWEEN 18 and 23;
+----------+------+| fname | age |+----------+------+| Sam | 25 || Diego | 17 || Angelina | 26 || Dan | 27 || Mary | 16 || Lena | 31 |+----------+------+
Shows students whose age is not in between 18 and 23.
Side note
When using BETWEEN with date of time, use CAST() to explicitly convert the values to the desired data types. For example, convert DATE value to DATETIME value for comparison
CAST()
Converts data types into desired ones
SELECT CAST('2011-05-05' AS DATETIME);
+--------------------------------+| CAST('2011-05-05' AS DATETIME) |+--------------------------------+| 2011-05-05 00:00:00 |+--------------------------------+
Converts string to DATETIME data type
Example
+-------+------------+-----------+---------------------+| name | birthdate | birthtime | birthdt |+-------+------------+-----------+---------------------+| Paul | 1956-11-13 | 10:03:33 | 1956-11-13 10:03:33 || Larry | 1967-12-23 | 04:20:47 | 1967-12-23 04:20:47 || John | 2019-09-29 | 00:44:35 | 2019-09-29 00:44:35 |+-------+------------+-----------+---------------------+
SELECT name, birthdt FROM pplWHERE birthdt BETWEEN '1960-01-01' AND '2000-03-03';
Above query sill works because MySQL is smart enough to auto convert string to correct data type
SELECT name, birthdt FROM pplWHERE birthdt BETWEEN CAST('1960-01-01' AS DATETIME)AND CAST('2000-03-03' AS DATETIME);
This one is correct
+-------+---------------------+| name | birthdt |+-------+---------------------+| Larry | 1967-12-23 04:20:47 |+-------+---------------------+
IN & NOT IN
SELECT fname FROM studentsWHERE fname='ellis' || fname='john' || fname='diego';-- SAME AS..SELECT fname FROM studentsWHERE fname IN ('ellis', 'john', 'diego');
+-------+| fname |+-------+| Ellis || John || Diego || Ellis |+-------+
Both do the same. Using IN is more concise
SELECT age from studentsWHERE ageNOT IN(16, 18, 20, 22, 24, 26, 28);-- SAME result AS..SELECT age from studentsWHERE age % 2 != 0;
+------+| age |+------+| 25 || 19 || 23 || 17 || 27 || 31 |+------+
Both yields same results
CASE
SELECT fname, lname, age ,CASE WHEN age >=10 THEN 'Teenage' WHEN age >=20 THEN 'Twenties' ELSE 'somethingElse' END AS agetypeFROM students;
+----------+----------+------+---------+| fname | lname | age | agetype |+----------+----------+------+---------+| Ellis | Min | 18 | Teenage || Tenzin | Pema | 22 | Teenage || Sam | Smith | 25 | Teenage || John | Johnson | 19 | Teenage || Dave | Smith | 23 | Teenage || Brent | Lang | 22 | Teenage || Jerimiah | Tucker | 20 | Teenage || Lamont | Marks | 18 | Teenage || Diego | Miller | 17 | Teenage || Angelina | Randall | 26 | Teenage || Brad | Johnson | 22 | Teenage || Dan | Joyce | 27 | Teenage || Mary | Fletcher | 16 | Teenage || Lena | Shah | 31 | Teenage || Ellis | Min | 18 | Teenage |+----------+----------+------+---------+
Above example is not expected. We can see that second WHEN is like else if statement. It won't be executed since the first statement was true
SELECT fname, lname, age ,CASE WHEN age BETWEEN 10 AND 19 THEN "Teenage" WHEN age BETWEEN 20 AND 29 THEN "Twenties" ELSE 'idk' END AS agetypeFROM students;-- MORE SUCCINTSELECT fname, lname, age ,CASE WHEN age < 20 THEN "Teenage" WHEN age < 30 THEN "Twenties" ELSE "idk" END AS agetypeFROM students;
+----------+----------+------+----------+| fname | lname | age | agetype |+----------+----------+------+----------+| Ellis | Min | 18 | Teenage || Tenzin | Pema | 22 | Twenties || Sam | Smith | 25 | Twenties || John | Johnson | 19 | Teenage || Dave | Smith | 23 | Twenties || Brent | Lang | 22 | Twenties || Jerimiah | Tucker | 20 | Twenties || Lamont | Marks | 18 | Teenage || Diego | Miller | 17 | Teenage || Angelina | Randall | 26 | Twenties || Brad | Johnson | 22 | Twenties || Dan | Joyce | 27 | Twenties || Mary | Fletcher | 16 | Teenage || Lena | Shah | 31 | idk || Ellis | Min | 18 | Teenage |+----------+----------+------+----------+
Below is more concise and preferred but be sure to avoid previous mistakes
WHEN && OR
SELECT fname, lname, CASE WHEN major='Biology' || major LIKE "%science" THEN "bio or science" ELSE "others" END AS "bio&science"FROM students;
+----------+----------+----------------+| fname | lname | bio&science |+----------+----------+----------------+| Ellis | Min | bio or science || Tenzin | Pema | bio or science || Sam | Smith | bio or science || John | Johnson | others || Dave | Smith | others || Brent | Lang | others || Jerimiah | Tucker | others || Lamont | Marks | others || Diego | Miller | others || Angelina | Randall | others || Brad | Johnson | bio or science || Dan | Joyce | others || Mary | Fletcher | others || Lena | Shah | bio or science || Ellis | Min | others |+----------+----------+----------------+
IF
SELECT fname, lname,IF(major="biology" || major LIKE "%science", "bio or science", "others") AS "bio&science"FROM students;
- Yields the same result as previous query
- IF(condition, then.., else)