Resources | Subject Notes | Computer Science
This section covers the fundamentals of databases and how to interact with them using Structured Query Language (SQL). We will focus on basic SQL queries, including SELECT
, WHERE
, AND
, OR
, and ORDER BY
.
A database is an organized collection of data, typically stored and accessed electronically. Databases allow for efficient storage, retrieval, and management of information.
Most modern databases are relational databases. This means data is organized into tables, which have rows (records) and columns (fields). Relationships between tables are established using common fields.
A table represents a collection of related data. Each row in a table represents a single record, and each column represents a specific attribute of that record.
Column Name | Data Type | Example |
---|---|---|
Student ID | Integer | 12345 |
Name | Text | Alice Smith |
Age | Integer | 16 |
SQL (Structured Query Language) is the standard language for interacting with relational databases. We will explore the core commands for retrieving data.
The SELECT
statement is used to retrieve data from one or more tables. The basic syntax is:
SELECT column1, column2, ...
FROM table_name;
To select all columns from a table, use SELECT *
.
The WHERE
clause is used to filter the data retrieved by specifying conditions. It allows you to select only rows that meet certain criteria.
Syntax: WHERE condition
Example: SELECT * FROM Students WHERE Age > 15;
(selects all students older than 15)
You can combine multiple conditions using logical operators:
AND
: Both conditions must be true.OR
: At least one condition must be true.Example: SELECT * FROM Students WHERE Age > 15 AND Grade = 'A';
(selects students older than 15 who have a grade of 'A')
Example: SELECT * FROM Students WHERE Age > 15 OR Grade = 'A';
(selects students older than 15 or those with a grade of 'A')
The ORDER BY
clause is used to sort the retrieved data. You can sort by one or more columns in ascending (ASC) or descending (DESC) order.
Syntax: ORDER BY column_name [ASC | DESC]
Example: SELECT * FROM Students ORDER BY Age ASC;
(sorts students by age in ascending order)
Example: SELECT * FROM Students ORDER BY Grade DESC, Name ASC;
(sorts students by grade in descending order, then by name in ascending order within each grade)
Query | Description |
---|---|
SELECT Name, Age FROM Students; |
Retrieves the name and age of all students. |
SELECT * FROM Students WHERE Age >= 16; |
Retrieves all information for students aged 16 or older. |
SELECT * FROM Students WHERE Age > 16 AND Grade = 'B'; |
Retrieves all information for students older than 16 with a grade of 'B'. |
SELECT * FROM Students WHERE Age > 16 OR Grade = 'B'; |
Retrieves all information for students older than 16 or those with a grade of 'B'. |
SELECT Name, Age FROM Students ORDER BY Age DESC; |
Retrieves the name and age of all students, sorted by age in descending order. |
SELECT * FROM Students ORDER BY Grade DESC, Name ASC; |
Retrieves all information for students, sorted by grade in descending order, and then by name in ascending order within each grade. |
This is a basic introduction to SQL queries. You can explore more advanced concepts such as joins, aggregate functions, and subqueries for a deeper understanding of database interaction.