SQL
Scalar Subquery
<div id="rrdiagram1"></div>  
Scalar subqueries are subqueries that return a single value. They can be used anywhere where an expression can be used. If a scalar subquery returns more than a single value, an error is raised (unless `scalar_subquery_error_on_multiple_rows` is set to `false`, in which case a row is selected randomly).  
Consider the following table:  
#### Grades {#docs:sql:expressions:subqueries::grades}  
<div class="narrow_table"></div>  
| grade | course |
|---:|:---|
| 7 | Math |
| 9 | Math |
| 8 | CS |  
```sql
CREATE TABLE grades (grade INTEGER, course VARCHAR);
INSERT INTO grades VALUES (7, 'Math'), (9, 'Math'), (8, 'CS');
```  
We can run the following query to obtain the minimum grade:  
```sql
SELECT min(grade) FROM grades;
```  
| min(grade) |
|-----------:|
| 7          |  
By using a scalar subquery in the `WHERE` clause, we can figure out for which course this grade was obtained:  
```sql
SELECT course FROM grades WHERE grade = (SELECT min(grade) FROM grades);
```  
| course |
|--------|
| Math   |