By plugging different numbers into a cell, you can quickly come up with different answers to a problem. A great example is using the PMT function with different interest rates and loan periods (in months) to figure out how much of a loan you can afford for a home or a car. You enter your numbers into a range of cells called a data table.
Here, the data table is the range of cells B2:D8. You can change the value in B4, the loan amount, and the monthly payments in column D automatically update. Using a 3.75% interest rate, D2 returns a monthly payment of $1,042.01 using this formula: =PMT(C2/12,$B$3,$B$4).
You can use one or two variables, depending on the number of variables and formulas you want to test.
Use a one-variable test to see how different values of one variable in a formula will change the results. For example, you can change the interest rate for a monthly mortgage payment by using the PMT function. You enter the variable values (the interest rates) in one column or row, and the outcomes are displayed in a nearby column or row.
In this live workbook, cell D2 contains the payment formula =PMT(C2/12,$B$3,$B$4). Cell B3 is the variable cell, where you can plug in a different term length (number of monthly payment periods). In cell D2, the PMT function plugs in the interest rate 3.75%/12, 360 months, and a $225,000 loan, and calculates a $1,042.01 monthly payment.
Use a two-variable test to see how different values of two variables in a formula will change the results. For example, you can test different combinations of interest rates and number of monthly payment periods to calculate a mortgage payment.
In this live workbook, cell C3 contains the payment formula, =PMT($B$3/12,$B$2,B4), which uses two variable cells, B2 and B3. In cell C2, the PMT function plugs in the interest rate 3.875%/12, 360 months, and a $225,000 loan, and calculates a $1,058.03 monthly payment.
Dessert (100g serving) |
Calories |
Fat (g) |
Carbs (g) |
Protein (g) |
---|---|---|---|---|
Frozen yoghurt |
159 |
6 |
24 |
4 |
Ice cream sandwich |
237 |
9 |
37 |
4.3 |
Eclair |
262 |
16 |
24 |
6 |
Cupcake |
305 |
3.7 |
67 |
4.3 |
Gingerbread |
356 |
16 |
49 |
3.9 |
\
The WHERE
clause is used to filter records.
It is used to extract only those records that fulfill a specified condition.
Select all customers from Mexico:
SELECT * FROM Customers
WHERE Country='Mexico';
SELECT
column1,
column2, ...FROM
table_nameWHERE
condition;
Note: The WHERE
clause is not only used in SELECT
statements, it is also used in UPDATE
, DELETE
, etc.!
Below is a selection from the Customers table used in the examples:
CustomerID |
CustomerName |
ContactName |
Address |
City |
PostalCode |
Country |
---|---|---|---|---|---|---|
1 |
Alfreds Futterkiste |
Maria Anders |
Obere Str. 57 |
Berlin |
12209 |
Germany |
2 |
Ana Trujillo Emparedados y helados |
Ana Trujillo |
Avda. de la Constitucin 2222 |
Mxico D.F. |
05021 |
Mexico |
3 |
Antonio Moreno Taquera |
Antonio Moreno |
Mataderos 2312 |
Mxico D.F. |
05023 |
Mexico |
4 |
Around the Horn |
Thomas Hardy |
120 Hanover Sq. |
London |
WA1 1DP |
UK |
5 |
Berglunds snabbkp |
Christina Berglund |
Berguvsvgen 8 |
Lule |
S-958 22 |
Sweden |
SQL requires single quotes around text values (most database systems will also allow double quotes).
However, numeric fields should not be enclosed in quotes:
SELECT * FROM Customers
WHERE CustomerID=1;
You can use other operators than the =
operator to filter the search.
Select all customers with a CustomerID greater than 80:
SELECT * FROM Customers
WHERE CustomerID > 80;
The following operators can be used in the WHERE
clause:
Operator |
Description |
Example |
---|---|---|
= |
Equal |
|
> |
Greater than |
|
< |
Less than |
|
>= |
Greater than or equal |
|
<= |
Less than or equal |
|
<> |
Not equal. Note: In some versions of SQL this operator may be written as != |
|
BETWEEN |
Between a certain range |
|
LIKE |
Search for a pattern |
|
IN |
To specify multiple possible values for a column |
Price: