You are on page 1of 3

Lab 3

Learning objectives:
 Use arithmetic operators in SQL statements
 Select rows from a table with conditional restrictions
 Apply logical operators to have multiple conditions

3.1 MySQL Arithmetic Operators


MYSQL ARITHMETIC OPERATORS OPERATION EXAMPLE
+ Addition Operator SELECT 10 + 2 = 12
– Subtraction Operator SELECT 10 – 2 = 8
* Multiplication Operator SELECT 10 * 2 = 20
/ Division Operator SELECT 10 / 2 = 5
% or MOD Modulus Operator SELECT 10 % 2 = 0
SELECT 10 MOD 3 = 1

3.2 Select rows from a table with conditional restrictions

3.2.1 SELECT Statement


The SELECT statement is used to select data from a database. The data returned is stored in a result
table, called the result-set.

Syntax
SELECT column1, column2, ...
FROM table_name;

First select “Northwind” database to use for statement to work.


USE Northwind;

Select "CustomerName" and "City" columns from the "Customers" table:


SELECT CustomerName, City FROM Customers;

3.2.2 SELECT DISTINCT Statement


The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a
column often contains many duplicate values; and sometimes you only want to list the different
(distinct) values.

Syntax
SELECT DISTINCT column1, column2, ...
FROM table_name;

Select ALL (including the duplicates) values from the "Country" column in the "Customers" table.
SELECT Country FROM Customers;

Select only the DISTINCT values from the "Country" column in the "Customers" table:
SELECT DISTINCT Country FROM Customers;

3.2.3 WHERE Clause


Extract only those records that fulfill a specified condition.

Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Select all the customers from the country "Mexico", in the "Customers" table:
SELECT * FROM Customers
WHERE Country='Mexico';

Text Fields vs. Numeric Fields


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 the customer with a specific customer ID from the "Customers" table:
SELECT * FROM Customers
WHERE CustomerID=1;

3.2.4 Operators in The WHERE Clause


= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal
BETWEEN Between a certain range
LIKE Search for a pattern
IN To specify multiple possible values for a column

3.3 Apply logical operators to have multiple conditions

AND, OR and NOT Operators


 The WHERE clause can be combined with AND, OR, and NOT operators.
 The AND and OR operators are used to filter records based on more than one condition:
o The AND operator displays a record if all the conditions separated by AND are TRUE.
o The OR operator displays a record if any of the conditions separated by OR is TRUE.
 The NOT operator displays a record if the condition(s) is NOT TRUE.

3.3.1 AND operator


Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

Select all fields from "Customers" where country is "Germany" AND city is "Berlin":
SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';

3.3.2 OR operator
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

Select all fields from "Customers" where city is "Berlin" OR "London":


SELECT * FROM Customers
WHERE City='Berlin' OR City='London';

3.3.3 NOT operator


Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

Select all fields from "Customers" where country is NOT "Germany":


SELECT * FROM Customers
WHERE NOT Country='Germany';

3.3.4 Combining AND, OR and NOT

Select all fields from "Customers" where country is "Germany" AND city must be "Berlin" OR
"London" (use parenthesis to form complex expressions):
SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR City='London');

Select all fields from "Customers" where country is NOT "Germany" and NOT "USA":
SELECT * FROM Customers
WHERE NOT Country='Germany' AND NOT Country='USA';

You might also like