SQL Basics
SQL Basics
08 Handout 1 *Property of ST
[email protected] Page 1 of 3
IT1924
• The most commonly used date functions are as follows: Example 2: SELECT ROUND(234.459, 2);
o YEAR – returns a four-digit year Result: 234.460
o MONTH – returns the number of the month o CEILING – returns the smallest integer value that is greater
o DAY – returns the number of the day than or equal to a number
Example: SELECT OrderID, YEAR(OrderDate) AS Year, Example: SELECT CEILING(234.1);
MONTH(OrderDate) AS Month, DAY(OrderDate) AS Day Result: 235
FROM Orders; o FLOOR - returns the largest integer value that is less than or
Result of query: equal to a number
OrderID Year Month Day Example: SELECT FLOOR(234.5);
1 2020 2 14 Result: 234
2 2020 2 14 • The most commonly used string functions are as follows:
3 2020 2 14 o CONCAT – joins two or more strings together
4 2020 2 15 Syntax: CONCAT(string1, string2, …);
o GETDATE – returns the current date and time Example: SELECT CONCAT(FirstName + ' ' , LastName)
Syntax: SELECT GETDATE(); FROM Customers WHERE CustomerID = 3446;
o DATEADD – adds a number of selected time/date periods to Result: Marco Velasco
a date then returns the date o LOWER – returns a string in lowercase letters
Syntax: SELECT DATEADD(datepart, number, date); o UPPER – returns a string in all capital letters
Example 1: SELECT DATEADD(month, 2, '2020/12/14'); Example: SELECT UPPER('sql') + ' ' + LOWER('FUNCTIONS');
Result: 2021-02-14 00:00:00.000 Result: SQL functions
Example 2: SELECT DATEADD(quarter, 2, '2020/02/14'); o SUBSTRING – returns a part of a string
Result: 2020-08-14 00:00:00.000 Syntax: SUBSTRING(string, start position, length);
Example 3: SELECT DATEADD(month, -2, '2020/02/14'); Example: SELECT SUBSTRING('SQL Functions', 1, 3);
Result: 2019-12-14 00:00:00.000 Result: SQL
Example 4: SELECT DATEADD(hour, 2, DATEADD(minute, o LEN – returns the number of characters in a string
30, '2020/02/14')); Example 1: SELECT LEN('SQL Functions');
Result: 2020-02-14 02:30:00.000 Result: 13
o DATEDIFF – returns the difference between two (2) dates Example 2: SELECT LEN('SQL Functions ');
Syntax: DATEDIFF(datepart, date1, date2); Result: 13
Example: SELECT DATEDIFF(month, '1989-07-25', '2020-02- o TRIM – removes the spaces or specific characters from start
14'); or end of a string
Result: 367 Syntax: TRIM([characters FROM ]string);
• The following are some of the most used numeric functions: Example: SELECT TRIM('<!> ' FROM ' <Functions! >');
o ABS – returns the absolute value of a number Result: Functions
Example: SELECT ABS(-234.5);
Result: 234.5
o ROUND – rounds a number to a specified number of decimal
places
Syntax: ROUND(number, decimal places);
Example 1: SELECT ROUND(234.459, 1);
Result: 234.500
08 Handout 1 *Property of ST
[email protected] Page 2 of 3
IT1924
SQL Joins 3458 Markus NULL
• A JOIN clause combines rows from two or more tables based on a 3464 Miguel NULL
common column. 3472 Martin 3
• The types of join are as follows:
o INNER JOIN – returns rows that have matching values in both • RIGHT JOIN
tables o Syntax: SELECT columns FROM table1 RIGHT JOIN table2
Outer Joins ON table1.column_name = table2.column_name;
o LEFT JOIN – returns all rows from the left table and the o Example: SELECT Customers.CustomerID,
matched records from the right table. If there is no match from Customers.FirstName, Orders.OrderID FROM Customers
the right side, the result is NULL. RIGHT JOIN Orders ON Customers.CustomerID =
o RIGHT JOIN – returns all rows from the right table and the Orders.CustomerID;
matched records from the left table. If there is no match from o Result of query:
the left side, the result is NULL. CustomerID FirstName OrderID
o FULL JOIN – returns all records when there is a match on 3446 Marco 1
either the left or right table. If there is no match, the missing 3425 Mark 2
side will contain NULL. 3472 Martin 3
• INNER JOIN NULL NULL 4
o Syntax: SELECT columns FROM table1 INNER JOIN table2
ON table1.column_name = table2.column_name; • FULL JOIN
o Example: SELECT Customers.CustomerID, o Syntax: SELECT columns FROM table1 FULL OUTER JOIN
Customers.FirstName, Orders.OrderID FROM Customers table2 ON table1.column_name = table2.column_name;
INNER JOIN Orders ON Customers.CustomerID = o Example: SELECT Customers.CustomerID,
Orders.CustomerID; Customers.FirstName, Orders.OrderID FROM Customers
o Result of query: FULL JOIN Orders ON Customers.CustomerID =
CustomerID FirstName OrderID Orders.CustomerID;
3425 Mark 2 o Result of query:
3446 Marco 1 CustomerID FirstName OrderID
3472 Martin 3 3425 Mark 2
3446 Marco 1
• LEFT JOIN 3458 Markus NULL
o Syntax: SELECT columns FROM table1 LEFT JOIN table2 3464 Miguel NULL
ON table1.column_name = table2.column_name; 3472 Martin 3
o Example: SELECT Customers.CustomerID, NULL NULL 4
Customers.FirstName, Orders.OrderID FROM Customers
LEFT JOIN Orders ON Customers.CustomerID =
Orders.CustomerID; References:
Coronel, C. and Morris, S. (2017). Database systems: design, implementation, and management
o Result of query: (12th ed.). USA: Cengage Learning.
CustomerID FirstName OrderID Elmasri, R. and Navathe, S. (2016). Fundamentals of database systems (7th ed.). USA: Pearson
3425 Mark 2 Higher Education.
Kroenke, D. and Auer, D. (2016). Database processing: fundamentals, design, and implementation.
3446 Marco 1 England: Pearson Education Limited.
08 Handout 1 *Property of ST
[email protected] Page 3 of 3