Com204 Lec7
Com204 Lec7
Database Principles: Fundamentals of Design, Implementation, and Management, 10 th Edition, Coronel, Morris, & Rob
Arithmetic Operators:
The Rule of Precedence
An easy way to remember the order of
precedence is PEMDAS
1. Parentheses, exponents, and roots.
2. Multiplication and division.
3. Addition and subtraction
COM304 Databases 2
Arithmetic Operators
COM304 Databases 3
The Rule of Precedence
• The application of this rule will show that:
– 8 + 2 * 5 = 8 + 10 = 18
– (8 + 2) * 5 = 10 * 5 = 50
• Similarly
– 4 + 5^2 * 3 = 4 + 25 * 3 = 4 + 75 = 79
– (4 + 5)^2 * 3 = 243
COM304 Databases 4
Example
• The prices of all the paintings with PTR_NUM 126
have all been increased by a certain amount because
the painter has become more famous so in order to
purchase any of his paintings, an additional N200
must be added to the original price. Select the
painting name, old price and new price(including the
N200 increment).
– SELECT PTNG_TITLE, PTNG_PRICE,
PTNG_NEW_PRICE=PTNG_PRICE+200
FROM PAINTING
WHERE PTR_NUM = 126;
COM304 Databases 5
Special Operators
• BETWEEN
– Select values within a given range. The values can be
numbers, text, or dates
– begin and end values are included.
– Syntax: SELECT * FROM [Table] WHERE [Column]
BETWEEN [value] AND [value];
• IS NULL
– Used to check whether attribute value is null
– Syntax: SELECT * FROM [Table] WHERE [Column] IS
NULL;
COM304 Databases 6
Special Operators
• LIKE
– Used to check whether attribute value matches
given string pattern
– Syntax: SELECT * FROM [Table] WHERE [Column]
starting with "a":
• JOIN
– combine rows from two or more tables,
based on a related column between
them(FK)
COM304 Databases 7
Special Operators (continued)
• IN
– Used to check whether attribute value matches
any value within a value list
– Syntax: SELECT * FROM [Table]
WHERE [Column] IN (‘value1’, ‘value2', ‘valuen’);
• LIMIT
– Used to specify the number of records to
return.
– Syntax: SELECT * FROM [Table] LIMIT [INT];
COM304 Databases 8
Additional Select Query Keywords
COM304 Databases 9
Aggregate Functions
COM304 Databases 10
Grouping Data
• The GROUP BY statement is often used with
aggregate functions (COUNT(), MAX(), MIN(),
SUM(), AVG()) to group the result-set by one
or more columns.
• For example when you want to count and
display how many paintings each painter has
– SELECT PTR_NUM, COUNT(PTNG_NUM) as
No_OF_PAINTINGS
FROM PAINTING
GROUP BY PTR_NUM;
COM304 Databases 11
HAVING Clause
COM304 Databases 12
HAVING Clause Example
• To select the number of paintings done by the
painters and where the number of paintings is
more than 2
SELECT PTR_NUM,
No_OF_PAINTINGS=COUNT(PTNG_NUM)
FROM PAINTING
GROUP BY PTR_NUM
HAVING COUNT(PTNG_NUM) > 2;
COM304 Databases 13
Date Formats
• DATE - YYYY-MM-DD
• DATETIME - YYYY-MM-DD HH:MI:SS
• TIMESTAMP - YYYY-MM-DD HH:MI:SS
• YEAR - YYYY or YY
• Ensure that the format of the date you are trying to
insert matches the format of the date column in the
database.
• Note: For simplicity, avoid time components
COM304 Databases 14
Date Functions
• NOW(): Returns the current date and time
• CURDATE(): Returns the current date
• CURTIME(): Returns the current time
• DATE(): Extracts the date part of a date or date/time
expression
• Syntax: SELECT [col], DATE([col]) AS [alias] FROM [table];
• EXTRACT(): Returns a single part of a date/time
• Syntax: SELECT [col], EXTRACT([UNIT FROM col]) AS
[alias] FROM [table];
• DATEDIFF(): Returns the number of days between two dates
• Syntax: SELECT [col], DATEDIFF(date2,date1) AS [alias] FROM [table];
COM304 Databases 15
Questions
COM304 Databases 16