0% found this document useful (0 votes)
12 views

SQL Filtering and Subqueries

Uploaded by

Divyansh Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

SQL Filtering and Subqueries

Uploaded by

Divyansh Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL - 02: Filtering and Subqueries

Tuesday, August 16, 2022 6:25 PM

Content covered in lecture:


• ROUND()
• CONCAT() [Alt: ||]
• ORDER BY
• WHERE
• BETWEEN
• IN
• LIKE (% and _)
• TRIM
• IS NULL
• Inner queries
• CASE statement

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

• We can order by columns not present in the select statement, as long as they're present in the table
• The order by can also be a custom expression that can be calculated for each row, it need not be a static column in the table, order by can also be
a number (1,2,3...) which indicates the column on which we need to sort (this positive number should be <= number of columns selected after
SELECT)

This indicates sort by the 1st column after select which in this case is name

This gives error as enough number of columns are not present after select
This sorts by dept

Sorting by any other value (-1, -2, -100, 'adasd', '') sorts the output by the primary key

• How WHERE works in backend?


○ When you write:
SELECT name FROM students
WHERE id='10'

The clause id='10' is executed for each row in the backend and a boolean mask of [True, False, True, True …] is generated
Then all the rows which have the True mask are returned
(similar to how pandas work in filtering)

Hence we can also use statements like:


SELECT name FROM students
WHERE 1

To get all rows

And statements like:


SELECT name FROM students
WHERE 0

To get nothing
• MySQL is not case sensitive with strings but other SQLs can be

• If we want MySQL to be case sensitive with strings:

• BETWEEN:
○ Both start and end are included
○ Can also work on strings:

• LIKE:
○ You can use LIKE with numbers/floats along with strings
○ You can't use like with multiple strings (as we can do with IN operator)
○ Like/= operators can't operator on NULL operator

• Inner queries

After IN, the selected column's name need not be the same as that before IN
It only checks for the values

• CASE

Syntax:
CASE
WHEN <bool_condition_1> THEN <value_to_be_present_when_bool_condition_1_is_true>
WHEN <bool_condition_2> THEN <value_to_be_present_when_bool_condition_2_is_true>
WHEN <bool_condition_3> THEN <value_to_be_present_when_bool_condition_3_is_true>
...
ELSE <value_to_be_present_when_all_bool_conditions_are_false>
END

If we don't mention an explicit ELSE statement, SQL will insert NULLs in place of ELSE
This Case statement will be applied to each row of the table and corresponding output will be generated
It is like IF-ELSE statement in Python

You might also like