Skip to content

Commit 462dd9d

Browse files
Merge pull request #171 from alexgeer/main
2 parents dbd5fb3 + 2010064 commit 462dd9d

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

docs/sql/sql-language.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,116 @@ Total cost of all products:
283283
- [SQLBolt - Queries with aggregates - Pt. 1](https://sqlbolt.com/lesson/select_queries_with_aggregates)
284284
- [SQLite Tutorial - SQLite Aggregate Functions](https://www.sqlitetutorial.net/sqlite-aggregate-functions/)
285285

286+
## Arithmetic Operators
287+
288+
When writing queries, you may find that the table does not have a column for a certain value but has columns which could be used to calculate it. SQL supports arithmetic operations in selections and `WHERE` clauses. In a selection, this will add a column to the result which calculates the expression for each result.
289+
290+
**Raw SQL**
291+
292+
```sql
293+
SELECT name, price, price * 0.07 FROM products;
294+
295+
┌───────────┬──────────┬─────────────────┐
296+
│ name │ price │ price * 0.07
297+
├───────────┼──────────┼─────────────────┤
298+
│ cookies │ 10.0010.70
299+
│ ice cream │ 5.005.35
300+
│ donuts │ 7.007.49
301+
└───────────┴──────────┴─────────────────┘
302+
303+
```
304+
305+
You can alias the calculated column using `AS` for a clearer output.
306+
307+
**Raw SQL**
308+
```sql
309+
SELECT name, price, price * 0.07 AS sales_tax FROM products;
310+
311+
┌───────────┬──────────┬─────────────────┐
312+
│ name │ price │ sales_tax │
313+
├───────────┼──────────┼─────────────────┤
314+
│ cookies │ 10.000.70
315+
│ ice cream │ 5.000.35
316+
│ donuts │ 7.000.49
317+
└───────────┴──────────┴─────────────────┘
318+
319+
```
320+
321+
The operations are also supported in `WHERE` clauses.
322+
323+
324+
```sql
325+
326+
-- select all rows where tax is greater than 10 dollars
327+
SELECT * FROM products WHERE price * 0.07 > 10.00
328+
```
329+
330+
## Subqueries
331+
332+
A subquery, also known as a nested query or inner query, is a query that is embedded within another SQL query. Subqueries allow you to retrieve data from one or more tables and use that result within another query. They are an essential feature of SQL for performing complex queries and data manipulation.
333+
334+
There are two primary types of subqueries:
335+
336+
1. **Scalar Subquery**:
337+
A scalar subquery is a subquery that returns a single value. This type of subquery is typically used within a `SELECT` or `WHERE` clause to compare a single value with the result of the subquery.
338+
```sql
339+
-- Find the average salary of employees
340+
SELECT AVG(salary) FROM employees;
341+
┌────────────────┐
342+
| AVG(salary) │
343+
├────────────────┤
344+
85000 |
345+
└────────────────┘
346+
347+
-- Use the above query in a subquery
348+
SELECT name, salary FROM employees
349+
WHERE salary > (SELECT AVG(salary) FROM employees);
350+
┌──────────┬─────────────┐
351+
│ name │ salary │
352+
├──────────┼─────────────┤
353+
│ Alice │ 100000
354+
│ Bob │ 90000
355+
│ Charlie │ 85001
356+
└──────────┴─────────────┘
357+
358+
```
359+
The subquery `SELECT AVG(salary) FROM employees` is a single, or scalar, value. When used in the `WHERE` clause, the full query finds all employees whose salary is greater than the average salary.
360+
361+
2. **Table Subquery**:
362+
A table subquery, also known as a derived table or inline view, returns a result set (a table) that can be used in a `FROM` clause or joined with other tables in the main query.
363+
```sql
364+
365+
-- Count the number of courses by the programming language they use
366+
SELECT language, COUNT(*) as num_courses
367+
FROM programming_courses GROUP BY language;
368+
┌──────────┬─────────────┐
369+
│ language │ num_courses │
370+
├──────────┼─────────────┤
371+
│ Python │ 10
372+
│ HTML/CSS │ 6
373+
│ SQL │ 3
374+
└──────────┴─────────────┘
375+
376+
377+
-- Using the above query in a subquery, we can use the MAX() function on the count column
378+
SELECT language, MAX(num_courses) FROM
379+
(SELECT language, COUNT(*) as num_courses
380+
FROM programming_courses GROUP BY language
381+
);
382+
┌──────────┬──────────────────┐
383+
│ language │ MAX(num_courses) │
384+
├──────────┼──────────────────┤
385+
│ Python │ 10
386+
└──────────┴──────────────────┘
387+
```
388+
389+
**Further Reading**
390+
391+
392+
- [SQL Subqueries - w3resource](https://www.w3resource.com/sql/subqueries/understanding-sql-subqueries.php)
393+
- [SQL - Sub Queries - TutorialsPoint](https://www.tutorialspoint.com/sql/sql-sub-queries.htm)
394+
395+
286396
## ALTER TABLE
287397

288398
After creating a table, you may need to add or rename a column. The `ALTER TABLE` command allows you to do this.

0 commit comments

Comments
 (0)