Top SQL Keywords and Functions: Master 80% of Data Engineering with 20% of SQL

Top SQL Keywords and Functions: Master 80% of Data Engineering with 20% of SQL
SQL is a fundamental skill for data engineering and analytics. With the right understanding of a few key concepts, you can handle most of the tasks required in data manipulation and analysis. Let’s dive into the details of the SQL keywords and functions mentioned earlier.
Sazit Suvo

Master the Most Important SQL Queries, Keywords, and Functions

SELECT

  • Allows fetching large datasets for analysis.
  • Use * to retrieve all co
SELECT name, age, salary
FROM employees;

WHERE

  • Helps in fetching only the required data by applying conditions.
  • Can use operators like =, <, >, <=, >=, !=, BETWEEN, LIKE, etc.
SELECT * 
FROM sales
WHERE region = 'North America' AND sale_date > '2023-01-01';

ORDER BY

  • Default sorting is in ascending order (ASC), but you can explicitly use DESC for descending order.
SELECT product_name, price
FROM products
ORDER BY price DESC;

Aggregation Functions (MIN, MAX, AVG, COUNT, SUM)

  • SUM: Adds values.
  • AVG: Calculates the average.
  • MIN & MAX: Find the smallest and largest values, respectively.
  • COUNT: Counts the number of rows.
SELECT COUNT(*) AS total_employees, AVG(salary) AS average_salary
FROM employees;

GROUP BY

  • Often used with aggregation functions.
  • Groups data based on one or more columns.
SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department;

JOINS (INNER, LEFT, RIGHT, FULL, SELF)

  • INNER JOIN: Returns rows with matching values in both tables.
  • LEFT JOIN: Returns all rows from the left table and matching rows from the right table.
  • RIGHT JOIN: Returns all rows from the right table and matching rows from the left table.
  • FULL JOIN: Returns all rows when there is a match in either table.
SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;

Common Table Expressions (CTE)

  • Improves readability and maintainability of SQL queries.
  • Defined using the WITH keyword.
WITH SalesCTE AS (
    SELECT region, SUM(sales) AS total_sales
    FROM sales
    GROUP BY region
)
SELECT *
FROM SalesCTE
WHERE total_sales > 100000;

Working with DATETIME/TIMESTAMP

  • Convert string dates to date format.
  • Extract parts like year, month, day, or calculate differences.
SELECT employee_name, EXTRACT(YEAR FROM hire_date) AS hire_year
FROM employees;

Numbering Functions (RANK, DENSE_RANK, ROW_NUMBER)

  • RANK: Assigns ranks with gaps for ties.
  • DENSE_RANK: Assigns consecutive ranks even with ties.
  • ROW_NUMBER: Assigns a unique row number without gaps.
SELECT name, salary, RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees;

Additional Keywords and Functions

SELECT name, IFNULL(bonus, 0) AS bonus
FROM employees;

CASTING

SELECT CAST(salary AS CHAR) AS salary_string
FROM employees;

MERGE STATEMENT

  • Purpose: Perform INSERT, UPDATE, or DELETE operations in a single query.
  • Example:
MERGE INTO employees AS target
USING new_employees AS source
ON target.id = source.id
WHEN MATCHED THEN UPDATE SET target.salary = source.salary
WHEN NOT MATCHED THEN INSERT (id, name, salary) VALUES (source.id, source.name, source.salary);

QUALIFY

  • Purpose: Filter rows after window functions are applied.
  • Example:
SELECT name, salary, RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees
QUALIFY rank <= 3;
administrator

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *