SQL Fundamentals

ksshravan
1 min readMay 25, 2024

--

This article covers various SQL topics, listed below

  • Selecting/renaming columns
  • Filtering rows
  • Handling null values
  • Handling date values
  • Aggregation and Group by
  • Joins (inner, left outer, self)
  • Union and union all
  • Query execution

Selecting/renaming columns

select name as emp_name, age
from employee_tbl

Handling null values

  • To get rows with null values, we use IS NULL in the where clause, not = NULL
  • Null values can be handled using either coalesce or ifnull
-- Get all rows with age not equal to 22 including rows with age as null
select *
from employee_tbl
where ifnull(age, 25) <> 22

select *
from employee_tbl
where coalesce(age, 25) <> 22
  • coalesce vs ifnull : is that ifnull takes 2 arguments, whereas coalesce can take 2 or more than 2 arguments. Ifnull returns the first argument if it is not null, and the second argument if the first argument is null. coalesce return the first non null argument in the list of arguments, and if all the arguments are null, it returns null

Query Execution

  • The order in which a query is logically executed is as follows:
    FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY -> LIMIT

--

--