Intro to JOINS: the AS PostgreSQL Statement
Before we examine JOINS, there’s a key foundation piece we must cover first: the AS statement in PostgreSQL. Let’s jump in.
About the AS Statement
The AS statement in PostgreSQL enables us to rename sections of a table or table columns with a temporary alias, or almost a variable, for manipulation.
It’s a simple statement, so let’s see it in action.
1. Basic AS Statement Example
Our most basic example is a basic query where perhaps a column wasn’t named to our liking. Consider the following.
SELECT rental_rate AS film_cost
Great for an introductory example, but not inherently useful. Read on as we apply the AS statement more deeply.
2. Semi-Intermediate AS Statement Example
Let’s provide an example that’s a bit more engaged. Example, if we use aggregate functions, the column output doesn’t have a clean name attached to it. But no longer! The AS statement allows us to have the summation output in a GROUP BY statement to something we’ll recognize.
SELECT customer_id, SUM(amount) AS customer_ltv
GROUP BY customer_id
ORDER BY customer_ltv DESC