Journal
Notes
The SQL series, grouped by the kind of question the note answers.
SQL
Joins
2 min
For Those Who Do Not Know JOIN
SQL is a very widespread and easy-to-use language because it is characterized by being close to the sentences we speak—close to our language. I will try to simplify the topic for you in general; it is easy, don't worry.
- JOIN
- primary key
- foreign key
- one-to-many
2 min
Understand Joins
Single table SQL select statements are rather easy to write. However, business requirements often dictate that more complex queries must be written. For example, "find all orders for each customer, and display the products for each order".
- inner join
- outer join
- equi-join
- customers and orders
2 min
Visual Examples of SQL Joins
Assume we have the following two tables. Table A is on the left, and Table B is on the right. We'll populate them with four records each.
- inner join
- left outer join
- full outer join
- Venn diagram
2 min
WHERE vs. ON clause
Does not matter for inner joins. Matters for outer joins.
- WHERE clause
- ON clause
- inner join
- outer join
5 min
The Difference Between AND and WHERE in Joins
I noticed that many people still don't understand the difference between using AND inside the ON clause versus using WHERE, so I wanted to simplify this for you here and give the topic the attention it deserves.
- AND
- ON clause
- WHERE
- outer join
1 min
How to Join Tables Using USING Instead of ON
The idea of joining tables is built on simplification and reducing row duplication. If you don't know how to use ON to join tables, check out this topic—it will help you: For Those Who Do Not Know JOIN
- USING
- ON
- shared column
- JOIN
2 min
Self JOIN
A self JOIN is a regular join, but the table is joined with itself.
- self join
- table alias
- reports to
- employees
3 min
Cross JOIN
The SQL CROSS JOIN produces a result set which is the number of rows in the first table multiplied by the number of rows in the second table if no WHERE clause is used along with CROSS JOIN. This kind of result is called as Cartesian Product.
- CROSS JOIN
- Cartesian product
- foods
- company
1 min
Use cases of Full Outer JOIN
Conceptually, a full outer join combines the effect of applying both left and right outer joins. Where rows in the FULL OUTER JOINed tables do not match, the result set will have NULL values for every column of the table that lacks a matching row.
- full outer join
- unmatched rows
- NULL
- departments
Filtering and sets
2 min
For Those Who Do Not Know DISTINCT
The DISTINCT keyword ignores duplicate rows so that each row is unique on its own. We only write it right after the SELECT keyword.
- DISTINCT
- duplicate rows
- SELECT
3 min
For Those Who Do Not Know LIKE and the Difference Between LIKE and IN
Of course, the first question you ask yourself is what wildcards are and what they mean.
- LIKE
- NOT LIKE
- wildcards
- IN
2 min
What You Do Not Know About OR, IN, and NOT IN
Suppose we have a table named kids, and we want to get the names of the kids who are causing trouble and breaking toys.
- OR
- IN
- NOT IN
3 min
For Those Who Do Not Know UNION and UNION ALL
I know we didn't cover this in the course content, but it's generally easy and it's better to know it in case you need it in the final exam.
- UNION
- UNION ALL
- duplicates
- set operators
2 min
For Those Who Do Not Know INTERSECT and EXCEPT
INTERSECT retrieves the common rows between both tables. EXCEPT retrieves the rows that are not present in the second table.
- INTERSECT
- EXCEPT
- set operators
- common rows
Aggregation
1 min
For Those Who Do Not Know GROUP BY
The GROUP BY clause groups duplicate values in a column. For example, if we have categorical nominal values like marital status (Married, Single, Divorced), and a table named person consisting of id, name, marital_status:
- GROUP BY
- COUNT
- AVG
- marital status
3 min
4 Ways to Use COUNT and When to Use Each
The COUNT() function is used to count the number of rows.
- COUNT(*)
- COUNT(column)
- COUNT DISTINCT
- NULL
3 min
GROUP BY with Aggregation Functions
GROUP BY clause can be used in a SELECT statement to collect data across multiple records and group the results by one or more columns.
- SUM
- MIN
- MAX
- COUNT
- GROUP BY
3 min
DISTINCT vs. GROUP BY
It’s worth noting that using DISTINCT, particularly in aggregations, can slow your queries down quite a bit.
- DISTINCT
- GROUP BY
- SUM
- unique values
2 min
3 Ways for Calculating Median in PostgreSQL
One quick note that a median might be a more appropriate measure of center for this data, but finding the median happens to be a pretty difficult thing to get using SQL alone — so difficult that finding a median is occasionally asked as an interview question.
- median
- PERCENTILE_CONT
- NTILE
- PostgreSQL
Functions and dates
1 min
For Those Who Do Not Know CAST
There are many cases where we need to convert columns from one data type to another data type. To be able to use CAST, you must first take a quick look at data types in SQL.
- CAST
- data types
- conversion
- ::
2 min
For Those Who Do Not Know COALESCE
The COALESCE function handles empty (NULL) values.
- COALESCE
- NULL
- default value
4 min
For Those Who Do Not Know SUBSTR, LEFT, RIGHT, STRPOS, and POSITION
The SUBSTR(), LEFT(), and RIGHT() functions are used to extract parts of text (strings).
- SUBSTR
- LEFT
- RIGHT
- STRPOS
- POSITION
1 min
For Those Who Do Not Know TO_DATE
The to_date function is used to convert data from a text/string type to a date type.
- TO_DATE
- string
- date
- format mask
2 min
DATE_TRUNC() vs. DATE_PART()
DATE_TRUNC allows you to truncate your date to a particular part of your date-time column. Common trunctions are day, month, and year.
- DATE_TRUNC
- DATE_PART
- month
- year
3 min
Ways of using CASE with SELECT statement
The CASE statement always goes in the SELECT clause.
- CASE
- WHEN
- THEN
- ELSE
Subqueries and CTEs
2 min
For Those Who Do Not Know Subquery
The word Subquery means a query inside a query, or an outer query with an inner query inside it.
- subquery
- inner query
- outer query
15 min
Subqueries (All You Need to Know)
In PostgreSQL, a subquery is a query within a query that allows you to construct complex queries. You can create subqueries within your SQL statements. These subqueries can reside in the WHERE clause, the FROM clause, or the SELECT clause.
- WHERE subquery
- IN
- EXISTS
- FROM clause
2 min
For Those Who Do Not Know WITH
First, we must understand that any query we write produces its own result table; that resulting table might not directly match the stored tables in the database, as we will see in this explanation.
- WITH
- result table
- subquery
3 min
WITH-queries vs. Subqueries
The WITH statement is often called a Common Table Expression or CTE. Though these expressions serve the exact same purpose as subqueries, they are more common in practice, as they tend to be cleaner for a future reader to follow the logic.
- CTE
- WITH
- subquery
- performance
Query structure
1 min
Order of clauses in SQL
Noting that the square brackets [ ] means optional.
- SELECT
- FROM
- WHERE
- GROUP BY
- ORDER BY
3 min
Why shouldn’t we use SELECT * queries
The common answer to why we should SELECT col1, ..., colN rather than SELECT * is that we reduce network traffic. In most cases, for reasonably normalized databases, and with today’s high-speed network connections, this is unlikely to be a huge deal.
- SELECT *
- covering index
- sequential scan
- network traffic
Data modeling
Setup and posting
1 min
For Beginners & Advanced SQL Series Index (EN & AR)
⚠️ Note: The Advanced Material section isn't related to the course materials of the initiative we have.
- SQL series
- beginners
- advanced material
- English guides
1 min
Most Common Commands You Will Need in PostgreSQL Shell
You can use SQL shell commands for PostgreSQL to display details about your database, its tables, and column properties.
- psql
- \dt
- \d
- \l
1 min
Import Parch & Posey on SQLite
It is handy and easy to use, and light weight which don't require to install a local server as well.
- SQLite
- DB Browser
- Parch and Posey
- CSV
1 min
SQL in Practice for Data Analytics
I have found good learning resources for practicing SQL and mastering it. It is a strong tool in data analysis field, and not all people are professional in it when It comes to Subqueries and such complex stuff. So I would like to share this learning resources with you
- DataCamp
- SQLZoo
- SQLBolt
- Mode
5 min
Data Analyst Nanodegree Prerequisites and Requirements
This guide is based on Nanodegree's prerequisite list
- Data Analyst Nanodegree
- Python
- statistics
- spreadsheets
2 min
Posting threads (Topics) and Best Practices
You decide to post a thread and want to learn best practices. You're in right place.
- forum threads
- DRY
- topic titles
- Discourse