0
0
Raspberry Piprogramming~20 mins

SQLite database for sensor data in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SQLite Sensor Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
What is the output of this SQLite query on sensor data?

Given a table sensors with columns id, temperature, and timestamp, what is the output of this query?

SELECT AVG(temperature) FROM sensors WHERE timestamp > '2024-01-01 00:00:00';

Assume the table has these rows:

  • (1, 20.5, '2023-12-31 23:59:59')
  • (2, 22.0, '2024-01-01 01:00:00')
  • (3, 21.5, '2024-01-02 12:00:00')
Raspberry Pi
SELECT AVG(temperature) FROM sensors WHERE timestamp > '2024-01-01 00:00:00';
A21.33
B21.75
C20.5
DNULL
Attempts:
2 left
💡 Hint

Only rows with timestamp after 2024-01-01 00:00:00 are included.

🧠 Conceptual
intermediate
1:00remaining
Which SQLite data type is best for storing sensor timestamps?

You want to store the exact time when sensor data was recorded. Which SQLite data type should you use?

AINTEGER storing Unix timestamps (seconds since 1970-01-01)
BBLOB storing binary time data
CREAL storing Julian day numbers
DTEXT storing ISO 8601 strings like '2024-06-01 12:30:00'
Attempts:
2 left
💡 Hint

Consider readability and SQLite's built-in date functions.

🔧 Debug
advanced
1:30remaining
Why does this SQLite INSERT fail for sensor data?

Consider this table creation and insert statements:

CREATE TABLE sensor_data (id INTEGER PRIMARY KEY, temperature REAL NOT NULL, timestamp TEXT NOT NULL UNIQUE);
INSERT INTO sensor_data (temperature, timestamp) VALUES (23.5, '2024-06-01 10:00:00');
INSERT INTO sensor_data (temperature, timestamp) VALUES (24.0, '2024-06-01 10:00:00');

Why does the second INSERT fail?

ABecause the timestamp column has a UNIQUE constraint and the second insert uses a duplicate timestamp
BBecause the timestamp format is invalid
CBecause id is not provided and must be manually inserted
DBecause temperature cannot be NULL and the second insert has NULL temperature
Attempts:
2 left
💡 Hint

Look at the constraints on the timestamp column.

📝 Syntax
advanced
1:30remaining
Which option correctly creates a SQLite table for sensor data with an auto-incrementing ID?

Choose the correct SQL statement to create a table sensor_readings with columns id (auto-incrementing primary key), humidity (REAL), and timestamp (TEXT).

ACREATE TABLE sensor_readings (id INTEGER PRIMARY KEY, humidity REAL, timestamp TEXT AUTOINCREMENT);
BCREATE TABLE sensor_readings (id INT AUTO_INCREMENT PRIMARY KEY, humidity REAL, timestamp TEXT);
CCREATE TABLE sensor_readings (id INTEGER PRIMARY KEY AUTOINCREMENT, humidity REAL, timestamp TEXT);
DCREATE TABLE sensor_readings (id INTEGER PRIMARY KEY, humidity REAL, timestamp TEXT DEFAULT CURRENT_TIMESTAMP AUTOINCREMENT);
Attempts:
2 left
💡 Hint

SQLite uses AUTOINCREMENT keyword differently than other SQL databases.

🚀 Application
expert
2:00remaining
How many rows will this SQLite query return for sensor data?

Given a table sensor_logs with columns id, temperature, and timestamp, what is the number of rows returned by this query?

SELECT DISTINCT temperature FROM sensor_logs WHERE temperature > 20 AND temperature < 25;

Assume the table has these rows:

  • (1, 21.0, '2024-06-01 08:00:00')
  • (2, 22.5, '2024-06-01 09:00:00')
  • (3, 21.0, '2024-06-01 10:00:00')
  • (4, 19.5, '2024-06-01 11:00:00')
  • (5, 24.9, '2024-06-01 12:00:00')
  • (6, 25.0, '2024-06-01 13:00:00')
A3
B4
C5
D6
Attempts:
2 left
💡 Hint

Remember DISTINCT removes duplicates and the temperature must be strictly greater than 20 and less than 25.