SELECT Statement
Sorting the Results using ORDER BY
WHERE is the new if
'WHERE' is the new 'if'
All languages I know use 'if' for conditions - except SQL. It uses where
for conditional statements. For example, let's say we need the details of just Bart...
If you want to find only the people with above average intelligence, try this query...
AND/OR
As is all major languages, you can combine conditions using AND/OR. Say you want all the male members with above average intelligence...
If you want the data of all people associated with the school, do this...
You can change the order of the statements using brackets...
Operaters
Operator | Meaning | Example |
---|---|---|
= | is equal to | name='Bart Simpson' |
!= | is not equal to | job != 'Student' |
< | is less than | iq < 100 |
<= | is less than or equal to | iq <= 100 |
> | is greater than | iq > 100 |
>= | is greater than or equal to | iq => 100 |
LIKE | string match | name LIKE '%Simpson' |
The LIKE operator matches string. For example, this query can be used to find all the Simpsons...
The '%' is a wild card - means any string(think '.*' if you know regular expression). 'Homer%' means that the string must start with 'Homer' - then anything goes. '%Simpson' means that the string must end with 'Simpson'. You can search for middle parts of a string as well...
There are a few other operators as well - like REGEXP. But the above once are all you need for most things.