Please enable JavaScript to use CodeHS

Data and Databases

Data in the real world

Databases are not just for classroom exercises. They are behind every app, game, social network, and online store. Understanding how code talks to databases will help you build better projects and make smarter choices about privacy and security.

Applications ~10 min Middle / High School
The Big Idea

Databases behind apps

Almost every app you use needs to remember things: your username, your settings, your messages, your scores. It stores those things in a database. When you open the app, it asks the database for the data it needs and shows it to you.

Even a simple to-do list has a database. Each to-do is a row with a title, a done/not-done flag, and maybe a due date. The app reads and updates that table every time you check something off.


How It Works

APIs and ORMs

Most apps do not write raw SQL directly from the user interface. Instead, the app talks to the database through an API or an ORM.

An API is a set of endpoints that the frontend calls. The backend then runs SQL on the database and returns the result. An ORM, or Object-Relational Mapper, lets programmers work with database rows as if they were objects in their programming language. Both make it safer and easier to avoid mistakes.

Example: A social media app might have an endpoint like /api/posts. The frontend requests it, the backend runs SELECT * FROM Posts, and the database sends back the data.


Beyond SQL

SQL vs NoSQL

Relational databases are not the only option. NoSQL databases store data in other ways, such as documents, key-value pairs, or graphs. They are often chosen when data is flexible or very large.

MongoDB stores documents that look like JSON. Redis stores key-value pairs very fast, often for caching. Graph databases store relationships between people or things. The right choice depends on the app.


Responsibility

Privacy and responsibility

When you store data about people, you have a responsibility to protect it. Personal information like names, emails, addresses, and passwords should be stored securely. Passwords should never be saved as plain text; they should be hashed using a one-way function.

You should also only collect the data you actually need. This is called data minimization. It lowers the risk if a breach ever happens and builds trust with your users. Always follow privacy laws and your school's or organization's policies.

Good habit: back up your database regularly and test restoring from the backup. Losing user data is one of the worst failures a project can have.


Interactive

App stack explorer

Press the buttons below to see how a request travels from a user click, through an API, to the database, and back. Notice what each layer does.

1. Frontend: user clicks "Load posts"
2. API: receives request, builds query
3. Database: runs SELECT * FROM Posts
4. Response: JSON data sent back
5. Frontend: displays posts
Waiting to start...

Code

Reading a CSV file

Much real-world data starts as a CSV file. This Python program reads a CSV, filters the rows, and prints a small report.


Your Turn

Check yourself

Pick an app you use. Name one piece of data it probably stores in a database and one thing it might need to keep private.

Your answer:

Sample answer: A messaging app stores messages, contacts, and passwords. Passwords and private messages must be protected with encryption or hashing.


Check Yourself

Quick quiz

1. What does an ORM do?

2. Why should passwords be hashed?

3. When might you choose NoSQL?

Keep learning

You have finished the Data and Databases series. Next, explore how computers talk to each other over networks.

Last tutorial in the Data and Databases series.