You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.07FROM products;
294
+
295
+
┌───────────┬──────────┬─────────────────┐
296
+
│ name │ price │ price *0.07 │
297
+
├───────────┼──────────┼─────────────────┤
298
+
│ cookies │ 10.00 │ 10.70 │
299
+
│ ice cream │ 5.00 │ 5.35 │
300
+
│ donuts │ 7.00 │ 7.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.07AS sales_tax FROM products;
310
+
311
+
┌───────────┬──────────┬─────────────────┐
312
+
│ name │ price │ sales_tax │
313
+
├───────────┼──────────┼─────────────────┤
314
+
│ cookies │ 10.00 │ 0.70 │
315
+
│ ice cream │ 5.00 │ 0.35 │
316
+
│ donuts │ 7.00 │ 0.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
+
SELECTAVG(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 > (SELECTAVG(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
0 commit comments