DEV Community

Samuel Mwai
Samuel Mwai

Posted on

# Introduction to SQL: DDL, DML, and Data Querying


In today's data-driven world, databases are used to store enormous amounts of information. Businesses use them to manage customers, transactions, employees, products, financial records, and many other types of data. To interact with these databases, one of the most important languages to learn is SQL, or Structured Query Language.

SQL allows users to create database structures, insert and modify information, and retrieve specific data for analysis. PostgreSQL, MySQL, SQL Server, and Oracle are examples of database systems that support SQL.

For anyone learning data science, data analytics, or database management, understanding the basic SQL commands is essential. Three important areas to understand are Data Definition Language (DDL), Data Manipulation Language (DML), and data querying.


1. Understanding DDL

[IMAGE: SQL DDL CREATE ALTER DROP TRUNCATE infographic]

DDL stands for Data Definition Language.

DDL is concerned with the structure of the database rather than the individual records stored inside it. It is used to create database objects and modify their structure.

Common DDL commands include:

  • CREATE
  • ALTER
  • DROP
  • TRUNCATE

CREATE

The CREATE command can be used to create a table.

CREATE TABLE customers (
    customer_id SERIAL PRIMARY KEY,
    name VARCHAR(100),
    age INT,
    city VARCHAR(50)
);
Enter fullscreen mode Exit fullscreen mode

This creates a customers table containing four columns.

ALTER

The ALTER command modifies an existing table.

For example, we can add an email column:

ALTER TABLE customers
ADD COLUMN email VARCHAR(100);
Enter fullscreen mode Exit fullscreen mode

DROP

DROP removes a database object.

DROP TABLE customers;
Enter fullscreen mode Exit fullscreen mode

This removes the entire customers table, including its structure and data.

TRUNCATE

TRUNCATE removes the rows from a table while keeping the table itself.

TRUNCATE TABLE customers;
Enter fullscreen mode Exit fullscreen mode

Therefore, DDL can be thought of as the part of SQL responsible for building and changing the database structure.


2. Understanding DML

[IMAGE: SQL DML INSERT UPDATE DELETE infographic]

DML stands for Data Manipulation Language.

While DDL deals primarily with the structure of the database, DML deals with the data stored inside tables.

The most common DML commands are:

  • INSERT
  • UPDATE
  • DELETE

INSERT

INSERT is used to add new records.

INSERT INTO customers (name, age, city)
VALUES ('Samuel', 25, 'Nairobi');
Enter fullscreen mode Exit fullscreen mode

This adds a new customer to the table.

Multiple records can also be inserted:

INSERT INTO customers (name, age, city)
VALUES
('John', 30, 'Mombasa'),
('Mary', 27, 'Kisumu'),
('Peter', 35, 'Nakuru');
Enter fullscreen mode Exit fullscreen mode

UPDATE

UPDATE changes existing records.

UPDATE customers
SET city = 'Nairobi'
WHERE customer_id = 2;
Enter fullscreen mode Exit fullscreen mode

The WHERE condition is extremely important because it specifies which record should be changed.

DELETE

DELETE removes records.

DELETE FROM customers
WHERE customer_id = 3;
Enter fullscreen mode Exit fullscreen mode

Again, the WHERE clause prevents you from accidentally deleting every record in the table.


3. Data Querying with SELECT

[IMAGE: SQL SELECT query and database results illustration]

One of the most important things you will do with SQL is query data.

A query is a request for information from a database. The SELECT statement is used to retrieve data from a table.

For example:

SELECT *
FROM customers;
Enter fullscreen mode Exit fullscreen mode

The * means that we want all columns.

You can also select only the columns you need:

SELECT name, city
FROM customers;
Enter fullscreen mode Exit fullscreen mode

This makes the result more focused and easier to analyze.


4. Filtering Data with WHERE

[IMAGE: SQL WHERE clause filtering rows diagram]

When working with large datasets, you often don't want every record. You may only want records that meet a particular condition.

The WHERE clause allows you to filter your results.

For example:

SELECT *
FROM customers
WHERE city = 'Nairobi';
Enter fullscreen mode Exit fullscreen mode

This returns only customers whose city is Nairobi.

You can also use comparison operators:

SELECT *
FROM customers
WHERE age > 30;
Enter fullscreen mode Exit fullscreen mode

SQL supports operators such as:

  • =
  • >
  • <
  • >=
  • <=
  • <>

You can also combine conditions using:

  • AND
  • OR
  • NOT

For example:

SELECT *
FROM customers
WHERE city = 'Nairobi'
AND age > 30;
Enter fullscreen mode Exit fullscreen mode

5. Sorting Data with ORDER BY

[IMAGE: SQL ORDER BY sorting data illustration]

Sometimes you want your results arranged in a particular order.

The ORDER BY clause allows you to sort the results.

For example:

SELECT *
FROM customers
ORDER BY age ASC;
Enter fullscreen mode Exit fullscreen mode

This sorts customers from the youngest to the oldest.

To sort from highest to lowest:

SELECT *
FROM customers
ORDER BY age DESC;
Enter fullscreen mode Exit fullscreen mode

ORDER BY is especially useful when analyzing rankings, sales, salaries, scores, or other numerical data.


6. GROUP BY and Aggregate Functions

[IMAGE: SQL GROUP BY COUNT SUM AVG visualization]

SQL is not only useful for retrieving individual records. It can also be used to summarize data.

Common aggregate functions include:

  • COUNT() — counts records
  • SUM() — calculates a total
  • AVG() — calculates an average
  • MIN() — finds the smallest value
  • MAX() — finds the largest value

For example:

SELECT COUNT(*)
FROM customers;
Enter fullscreen mode Exit fullscreen mode

This returns the total number of customers.

We can also calculate the average age:

SELECT AVG(age)
FROM customers;
Enter fullscreen mode Exit fullscreen mode

GROUP BY

GROUP BY allows us to organize records into groups.

For example, we can count customers by city:

SELECT city, COUNT(*)
FROM customers
GROUP BY city;
Enter fullscreen mode Exit fullscreen mode

The result could look like:

City Customer Count
Nairobi 25
Mombasa 12
Kisumu 8
Nakuru 15

This type of query is particularly useful in data analytics, because it allows large datasets to be summarized into meaningful information.


7. Putting SQL Together

[IMAGE: SQL query flow SELECT FROM WHERE ORDER BY LIMIT]

The real power of SQL comes from combining these commands.

Suppose we want to find the three oldest customers from Nairobi.

We could write:

SELECT name, age
FROM customers
WHERE city = 'Nairobi'
ORDER BY age DESC
LIMIT 3;
Enter fullscreen mode Exit fullscreen mode

Here, several SQL concepts work together:

SELECT   → Choose the columns
FROM     → Choose the table
WHERE    → Filter the records
ORDER BY → Sort the results
LIMIT    → Restrict the number of results
Enter fullscreen mode Exit fullscreen mode

This is the foundation of more advanced SQL queries, including joins, subqueries, common table expressions, and window functions.


8. DDL vs DML vs Data Querying

[IMAGE: SQL DDL DML DQL comparison infographic]

Category Purpose Common Commands
DDL Defines database structure CREATE, ALTER, DROP, TRUNCATE
DML Manipulates stored data INSERT, UPDATE, DELETE
Querying Retrieves information SELECT, WHERE, GROUP BY, ORDER BY

The easiest way to remember the difference is:

DDL → Structure

Build and modify the database structure.

DML → Data

Add, change, and remove records.

Querying → Information

Retrieve and analyze information stored in the database.


Conclusion

SQL is one of the fundamental skills for anyone working with databases and data. DDL provides the commands needed to create and modify database structures, while DML allows users to insert, update, and delete information. Data querying, particularly through SELECT, allows users to retrieve and analyze the information stored in those structures.

Once these fundamentals are understood, you can progress to more powerful SQL concepts such as JOINs, subqueries, CTEs, window functions, views, indexes, and advanced aggregation.

For a data analyst or data scientist, SQL is more than simply a database language—it is a practical tool for turning large amounts of raw data into information that can be analyzed and used for decision-making.

Summary

SQL provides a structured way to work with databases:

  • DDL manages database structure.
  • DML manages the data stored in tables.
  • SELECT retrieves information.
  • WHERE filters data.
  • ORDER BY sorts data.
  • GROUP BY organizes data into groups.
  • Aggregate functions summarize data.

Together, these concepts form the foundation for working with relational databases and provide an important starting point for anyone pursuing a career in data analytics, data science, or database management.

Top comments (0)