111763 Views
85221 Views
83639 Views
51774 Views
49949 Views
48472 Views
Obsidian - the best tool for Makers
10 Projects for your Raspberry Pi Pico
Raspberry Pi Telegraf Setup with Docker
Setting Up Dynamic DNS on a Raspberry Pi for Self-Hosting
Raspberry Pi WordPress Setup with Docker
Raspberry Pi WireGuard VPN Setup with Docker
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
40% Percent Complete
By Kevin McAleer, 3 Minutes
Module 7 delves into advanced SQL queries in SQLite. You will learn about aggregating data, using grouping and filtering conditions, and working with subqueries and nested queries.
The GROUP BY clause allows us to group rows based on specific columns and perform aggregate functions on those groups.
GROUP BY
# Grouping data example query = ''' SELECT genre, COUNT(*) AS count FROM books GROUP BY genre ''' result = connection.execute(query) data = result.fetchall()
In this example, we group books based on their genre and use the COUNT() function to count the number of books in each genre.
COUNT()
The HAVING clause allows us to filter groups based on specific conditions after the GROUP BY operation.
HAVING
# Filtering grouped data example query = ''' SELECT genre, COUNT(*) AS count FROM books GROUP BY genre HAVING COUNT(*) > 5 ''' result = connection.execute(query) data = result.fetchall()
In this example, we retrieve genres with more than 5 books by using the HAVING clause to filter the groups.
SQLite provides various built-in functions and expressions that allow us to perform calculations and transformations on data within queries.
# Using built-in functions example query = ''' SELECT AVG(price) AS average_price, MAX(price) AS max_price, MIN(price) AS min_price FROM books ''' result = connection.execute(query) data = result.fetchall()
In this example, we calculate the average, maximum, and minimum prices of books using the AVG(), MAX(), and MIN() functions.
AVG()
MAX()
MIN()
Subqueries, also known as nested queries, allow us to use the result of one query within another query.
# Subquery example query = ''' SELECT title, author FROM books WHERE genre IN ( SELECT genre FROM books WHERE year > 2000 ) ''' result = connection.execute(query) data = result.fetchall()
In this example, we retrieve the titles and authors of books that belong to genres found in a subquery. The subquery selects genres of books published after 2000.
SQLite supports advanced SQL techniques, including views, common table expressions (CTEs), and window functions. These techniques can provide powerful ways to structure and manipulate data in queries.
Views are virtual tables based on the result of a query. They allow us to simplify complex queries and reuse query logic.
CTEs are named temporary result sets that we can reference within a query. They provide a convenient way to break down complex queries into smaller, more manageable parts.
Window functions perform calculations across a set of rows in a query result. They allow us to perform calculations such as ranking, row numbering, and cumulative sums.
By mastering advanced SQL queries and techniques, you will have the skills to perform complex data analysis and manipulation within SQLite databases.
< Previous Next >