114532 Views
101685 Views
86270 Views
54891 Views
51137 Views
49962 Views
Level Up your CAD Skills
Operation Pico
Raspberry Pi Home Hub
Hacky Temperature and Humidity Sensor
Robot Makers Almanac
High Five Bot
Using the Raspberry Pi Pico's Built-in Temperature Sensor
Getting Started with SQL
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
KevsRobots Learning Platform
90% Percent Complete
By Kevin McAleer, 6 Minutes
In this lesson, we’ll explore SQL functions and expressions that enhance your ability to manipulate, analyze, and transform data. SQL provides a variety of functions, including string, numeric, date, and aggregate functions, that make it easier to handle different types of data efficiently.
SQL functions are built-in commands that perform specific operations on data and return a single result. Functions can be applied to columns in your database to manipulate or calculate values directly within your queries.
Aggregate functions operate on a set of values and return a single result. They are commonly used in conjunction with the GROUP BY clause.
GROUP BY
COUNT
SELECT COUNT(*) FROM users;
SUM
SELECT SUM(salary) FROM employees;
AVG
SELECT AVG(salary) FROM employees;
MIN
SELECT MIN(salary) FROM employees;
MAX
SELECT MAX(salary) FROM employees;
Example: Find the total salary and average salary of employees.
SELECT SUM(salary) AS total_salary, AVG(salary) AS average_salary FROM employees;
String functions allow you to manipulate text data. These functions are useful for formatting, concatenating, and altering strings.
CONCAT
SELECT CONCAT(first_name, ' ', last_name) FROM users;
LENGTH
SELECT LENGTH(name) FROM users;
UPPER
SELECT UPPER(name) FROM users;
LOWER
SELECT LOWER(name) FROM users;
SUBSTRING
SELECT SUBSTRING(name, 1, 3) FROM users;
Example: Retrieve employee names in uppercase and show the length of each name.
SELECT UPPER(name) AS upper_name, LENGTH(name) AS name_length FROM employees;
Numeric functions are used to perform mathematical operations on numeric data.
ROUND
SELECT ROUND(salary, 2) FROM employees;
CEIL
SELECT CEIL(salary) FROM employees;
FLOOR
SELECT FLOOR(salary) FROM employees;
ABS
SELECT ABS(salary - 50000) FROM employees;
Example: Round each employee’s salary to the nearest hundred.
SELECT name, ROUND(salary, -2) AS rounded_salary FROM employees;
Date functions allow you to manipulate and extract information from date and time data.
NOW
SELECT NOW();
CURDATE
SELECT CURDATE();
YEAR
SELECT YEAR(birth_date) FROM users;
MONTH
SELECT MONTH(birth_date) FROM users;
DAY
SELECT DAY(birth_date) FROM users;
DATEDIFF
SELECT DATEDIFF(NOW(), hire_date) FROM employees;
Example: Find the number of days each employee has been with the company, based on their hire date.
SELECT name, DATEDIFF(NOW(), hire_date) AS days_with_company FROM employees;
Expressions in SQL allow you to perform calculations or manipulate data directly in queries.
Example: Calculate a 10% bonus for each employee based on their current salary.
SELECT name, salary, salary * 0.10 AS bonus FROM employees;
Example: Combine first_name and last_name into a single full_name column.
first_name
last_name
full_name
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;
Example: Calculate each employee’s next anniversary by adding one year to their hire_date.
hire_date
SELECT name, hire_date, DATE_ADD(hire_date, INTERVAL 1 YEAR) AS next_anniversary FROM employees;
Calculate Total and Average Salary: Find the total and average salary of all employees.
String Manipulation: Retrieve the first three letters of each employee’s name and display it in uppercase.
SELECT UPPER(SUBSTRING(name, 1, 3)) AS name_prefix FROM employees;
Round Salaries: Display each employee’s salary rounded to the nearest thousand.
SELECT name, ROUND(salary, -3) AS rounded_salary FROM employees;
Date Difference: Calculate the number of days each employee has worked at the company based on their hire date.
Calculate Bonuses: Calculate a 5% bonus for each employee based on their salary.
SELECT name, salary, salary * 0.05 AS bonus FROM employees;
Here’s a summary of the SQL functions and expressions covered in this lesson:
+
-
*
/
With these SQL functions and expressions, you can perform a wide range of data manipulations directly within your queries. In the next lesson, we’ll wrap up the course with a final project to apply everything you’ve learned.
< Previous Next >