If youâre talking to your friend or a significant other and you want a sandwich, you donât typically give them step-by-step instructions on how to make the sandwich. If you did, it would feel like programming your friend. Instead, youâre far more likely to talk about the result you want, such as âPlease make me a sandwichâ (or, perhaps, âSudo make me a sandwichâ). If your friend is willing and able to follow this instruction, then they would translate the phrase âMake me a sandwichâ into a series of steps, such as finding a loaf of bread, removing two slices, applying toppings, etc.This type of result-focused instruction is how declarative programming works.
In SQL you "just" declare the nature of the results that you would like to get. Not how your computer shall compute those results. You're telling the "machine" what you would like to happen, and allowing the computer to figure out how to do it. It is the role of the query interpreter (SQL is a language so you can infer what an interpreter is) of the database system to design and perform a process to produce such a result. The problem is that most of us intuitively think in terms of imperative programming. Â As in: "machine, do this, and then do that, but before, run a check and fail if this-and-that". This includes storing temporary results in variables, writing loops, iterating, calling functions, etc. Forget about all that. Think about how to declare things. Not about how to tell the machine to compute things.
SQL is shorter and easier to read. Since we don't have to deal with the how we can focus on what and let the database optimize the how for us.
I'm So Aggravated by  Aggregate Data!
In statistics, aggregate data is data combined from several measurements. When data is aggregated, groups of observations are replaced with summary statistics based on those observations.  For example:
wizards| id | name | age | color | power_1 | power_2 | power_3
powers name | damage | wizard_id | ---------------------------------------| fire ball    |   7  |   1   || ice storm    |  13  |   3   | | shape shift   |   0  |   4   | | invisiblity   |   0  |   2   | | lightning bolt |  23  |   1   | | earth quake   |  15  |   2   | | tidal wave   |  44  |   4   | | teleport    |   0  |   3   | ---------------------------------------
We aggregate data using aggregate functions which operate on a group of values to produce a single, summarizing value. So it would require taking summary observations, in this case wizard powers and doing something with that information. Aggregate functions operate on a group of values to produce a single, summarizing value. We can now find the average, count or sum, along with other information.Â
Something I learned  through trial and error are some of the things that an aggregate expression can't do!Â
It canât appear in a WHERE clause. For example :
If you want to find the title of the book with the highest sales, you canât use:
SELECT title_id  FROM titles WHERE sales = MAX(sales); YOU CAN'T DO THIS!
Also you canât mix nonaggregate (row-by-row) and aggregate expressions in a SELECT clause. A SELECT clause must contain either all nonaggregate expressions or all aggregate expressions.Â
If you want to find the title of the book with the highest sales, you canât use:
SELECT title_id, MAX(sales)FROM titles; Â Â Â Â Â Â Â --Illegal DON'T DO IT!
You may think you're slick and try nesting functions. Nope! As much of a short cut as this may seem, you can't nest aggregate functions either !!
SELECT SUM(AVG(sales))FROM titles; Â Â Â Â Â Â Â --Illegal
Yet with all of this the use of aggregate data dramatically reduces the time to query large sets of data. So there's your take away !!
âA SQL query goes into a bar, walks up to two tables and asks, âCan I join you?â