Skip to content

Chapter 4 of 8

Querying and Modifying Data with DML

Data Manipulation Language, or DML, is the part of SQL used to read and change the data stored in tables. Its four main commands are SELECT for reading, INSERT for adding new rows, UPDATE for changing existing rows, and DELETE for removing rows. Although the syntax is simple, careful use of filtering clauses is essential to avoid unintended changes.

The SELECT statement retrieves data from one or more tables. A query like SELECT column1, column2 FROM table_name; returns specific columns, while SELECT * FROM users; returns every column. Rows can be filtered using a WHERE clause, which supports operators such as =, >, LIKE, and IN; for instance, SELECT * FROM users WHERE age > 18; returns only adults. Results can be sorted with ORDER BY, either ascending (ASC) or descending (DESC), and the number of rows returned can be limited with LIMIT, optionally combined with OFFSET for pagination. NULL values require special handling because they are not equal to anything: IS NULL and IS NOT NULL are used for testing, while functions like COALESCE(value, default) and IFNULL(value, default) substitute a fallback when a value is missing.

Adding new data uses the INSERT INTO statement, which can add a single row or multiple rows at once, such as INSERT INTO users (name, age) VALUES ('Alice', 25);. To change existing rows, the UPDATE statement modifies values, always paired with a WHERE clause to avoid rewriting every row in the table, as in UPDATE users SET age = 26 WHERE id = 1;. The DELETE FROM statement removes rows, again with a WHERE clause to target specific records; omitting the WHERE clause deletes every row in the table. Used together, these commands cover the full lifecycle of working with data.

All chapters
  1. 1Database Fundamentals and SQL
  2. 2Tables, Keys, and Data Types
  3. 3Defining Database Structure with DDL
  4. 4Querying and Modifying Data with DML
  5. 5Aggregates, Joins, and Subqueries
  6. 6Indexes, Views, and Advanced Features
  7. 7Transactions, ACID, and Normalization
  8. 8SQL versus NoSQL and Database Security

Drill it

Reading is not remembering. These come from the SQL And Databases deck:

Q

What is a database?

A database is an organized collection of structured data stored and accessed electronically from a computer system. Databases allow efficient storage, retrieval...

Q

What is a Relational Database Management System (RDBMS)?

An RDBMS is a software system that manages relational databases, organizing data into tables with rows and columns related by keys. It supports SQL for querying...

Q

What are the main types of databases?

Databases are broadly classified into relational (SQL-based, like MySQL) and non-relational (NoSQL, like MongoDB for documents or Cassandra for wide-column stor...

Q

What is SQL?

SQL stands for Structured Query Language, a standard language for managing and manipulating relational databases. It includes sublanguages like DDL for schema d...