Introduction
SQL stands for Structured Query Language. It is a programming language used to communicate with and manage data stored in relational databases. SQL allows users to create databases and tables, add and modify information, search for specific records, and organize data.
1. DDL AND DML
SQL commands are divided into different categories. Two important categories are DDL and DML.
DDL – Data Definition Language
DDL stands for Data Definition Language. It is used to create and modify the structure of a database and its objects, such as tables.
Common DDL commands include:
- CREATE – creates a new database object, such as a table.
- ALTER – changes the structure of an existing table.
- DROP – permanently removes a database object.
- TRUNCATE – removes all records from a table while keeping the table structure.
Example:
CREATE TABLE Students (
StudentID INT,
Name VARCHAR(50),
Age INT
);
This command creates a table called Students with three columns.
DML – Data Manipulation Language
DML stands for Data Manipulation Language. It is used to add, change, and remove data stored in database tables.
Common DML commands include:
- INSERT – adds new records.
- UPDATE – changes existing records.
- DELETE – removes records.
Example:
INSERT INTO Students (StudentID, Name, Age)
VALUES (1, 'Grace', 20);
This adds a new student record to the table.
Summary
DDL manages the structure of the database, while DML manages the data stored inside the database.
THE WHERE CLAUSE
The WHERE clause is used in SQL to specify a condition. It allows us to select only the records that meet a particular requirement.
For example:
SELECT *
FROM Students
WHERE Age = 20;
This query displays students whose age is 20.
The WHERE clause is useful when working with large databases because it allows specific records to be selected instead of displaying everything.
Operators Used with the WHERE Clause
1. Comparison Operators
These operators compare two values.
| Operator | Meaning | Example |
|---|---|---|
= |
Equal to | Age = 20 |
<> or !=
|
Not equal to | Age <> 20 |
> |
Greater than | Age > 18 |
< |
Less than | Age < 18 |
>= |
Greater than or equal to | Age >= 18 |
<= |
Less than or equal to | Age <= 18 |
2. Logical Operators
Logical operators combine or modify conditions.
- AND – both conditions must be true.
- OR – at least one condition must be true.
- NOT – reverses a condition.
Example:
SELECT *
FROM Students
WHERE Age >= 18 AND Age <= 25;
This selects students between the ages of 18 and 25.
3. BETWEEN
BETWEEN selects values within a specified range.
SELECT *
FROM Students
WHERE Age BETWEEN 18 AND 25;
4. IN
IN checks whether a value matches any value in a given list.
SELECT *
FROM Students
WHERE Age IN (18, 20, 22);
5. LIKE
LIKE is used to search for a particular pattern in text.
SELECT *
FROM Students
WHERE Name LIKE 'G%';
The % means that any number of characters can follow G.
6. IS NULL
IS NULL is used to find records where a column has no value.
SELECT *
FROM Students
WHERE Age IS NULL;
Summary
The WHERE clause helps filter database records. Its common operators include =, <>, !=, >, <, >=, <=, AND, OR, NOT, BETWEEN, IN, LIKE, and IS NULL.
CASE WHEN
CASE WHEN is used in SQL to perform conditional logic. It works similarly to an IF-ELSE statement in programming.
It checks a condition and returns a result when that condition is true.
Basic Structure
CASE
WHEN condition THEN result
ELSE result
END
Example
Suppose we have students and their marks. We can use CASE WHEN to classify their performance:
SELECT Name, Marks,
CASE
WHEN Marks >= 70 THEN 'Excellent'
WHEN Marks >= 50 THEN 'Pass'
ELSE 'Fail'
END AS Performance
FROM Students;
The query creates a new column called Performance.
For example:
| Name | Marks | Performance |
|---|---|---|
| Grace | 78 | Excellent |
| Brian | 56 | Pass |
| Jane | 42 | Fail |
Uses of CASE WHEN
CASE WHEN can be used to:
- Categorize data.
- Create labels.
- Group records based on conditions.
- Replace complicated conditional calculations.
- Make query results easier to understand.
Summary
CASE WHEN allows SQL to make decisions based on conditions and return different results depending on the data.
DATA QUERYING
Data querying is the process of requesting specific information from a database. SQL is mainly used for querying data through the SELECT statement.
Basic SELECT Query
SELECT *
FROM Students;
The * means that all columns should be displayed.
Selecting Specific Columns
Instead of selecting everything, we can select particular columns:
SELECT Name, Age
FROM Students;
This displays only the Name and Age columns.
Filtering Data
The WHERE clause can be used when querying specific records:
SELECT Name, Age
FROM Students
WHERE Age >= 18;
Sorting Data
The ORDER BY clause sorts query results.
SELECT Name, Marks
FROM Students
ORDER BY Marks DESC;
DESC sorts from highest to lowest, while ASC sorts from lowest to highest.
Combining Querying Techniques
SQL allows several clauses to be used together:
SELECT Name, Marks
FROM Students
WHERE Marks >= 50
ORDER BY Marks DESC;
This query:
- Selects the student's name and marks.
- Displays only students who scored 50 or above.
- Arranges the results from the highest mark to the lowest.
Summary
Data querying allows users to retrieve useful information from databases. Common SQL commands and clauses used for querying include SELECT, FROM, WHERE, ORDER BY, GROUP BY, and HAVING.
Conclusion
Learning SQL is especially useful in data analytics because it allows analysts to work directly with large amounts of structured data and extract the information needed for decision-making.
Top comments (0)