SQL stands for Structured Query Language. It is the standard language used to communicate with databases. Think of SQL as the "English" you speak to a database when you want to get information, add new data, update records, or delete something. SQL allows you to do 4 main things (often remembered as CRUD) where C represents Create, R represents READ while U represents Update and D represents Delete. However the SQL keyword for Create is INSERT, for Read is SELECT, for Update is UPDATE and for Delete is DELETE. SQL also useful in filtering data(WHERE), sorting data(ORDER BY), grouping data(GROUP BY, AVG, COUNT), Combining tables(JOIN) and advanced analysis(NTILE)
Learning SQL can be made simple by breaking down learning into key concepts making it much easier to understand and grasp how it works. In this guide, we'll cover fundamental topics that every beginner should know which include JOINS, compare Window Functions and GROUP BY and cover SQL functions every beginner should know.
SQL JOINs
A JOIN combines rows from two or more tables based on a related column. The tables in the join must have a column they share in common. There are different types of JOINS and each is unique based on the rows needed from the different tables. The joins include;
- INNER JOIN - returns only the rows that have matching values in both tables and row without a match are excluded from the result.
- LEFT JOIN (or LEFT OUTER JOIN) - Returns all rows from the left table plus matching rows from the right table. If no match exists, it returns NULL for columns from the right table.
- RIGHT JOIN (or RIGHT OUTER JOIN) - Returns all rows from the right table + matching rows from the left table. Opposite of LEFT JOIN.
- FULL OUTER JOIN (or FULL JOIN) - returns all rows from both tables. Matches where possible and puts NULL where there is no match.
Window Functions vs GROUP BY – What is the Difference?
SQL window functions and GROUP BY both perform calculations across related rows, but they solve different problems. GROUP BY summarises data by collapsing rows into one result per group, while Window Functions calculate values across a set of rows without removing the original row details. GROUP BY is commonly used for aggregation and reporting while Window Functions are for analysis while keeping detail. They let you calculate things like rank, totals, averages and still show each original row.
With GROUP BY, the output becomes smaller because rows are combined into groups. That means you usually cannot select non-grouped columns unless they are aggregated too. With window functions, the output keeps the same number of rows as the input, because the calculation is attached to each row using OVER(...). This makes it useful when you want both the row detail and the aggregated context in the same result.
SQL Functions Every Beginner Should Know
Here are the five functions I recommend learning first:
- ROUND(number, decimals) - Rounds numbers cleanly. ROUND(mark, 2) → 87.345 becomes 87.35
- CONCAT() or || (PostgreSQL string concatenation)Joins text together.first_name || ' ' || last_name → “John Smith”
- TO_CHAR(date, format) Formats dates beautifully.TO_CHAR(exam_date, 'Day, DDth Month YYYY') → “Friday, 15th March 2024”
Top comments (0)