Lesson 3 of 60 – Features of SQL
5%

Features of SQL

SQL provides a powerful and structured way to work with relational databases. It can be used to create databases and tables, store data, retrieve information, modify records, control access, and manage transactions.

Note: SQL is a standardized language, but individual database systems may provide additional features and syntax beyond the SQL standard.
1. Easy to Learn

SQL uses readable keywords such as SELECT, FROM, WHERE, INSERT, UPDATE, and DELETE. This makes basic SQL statements relatively easy to understand.

SELECT name
FROM students
WHERE course = 'Python';
2. Used for Data Retrieval

SQL can retrieve specific information from one or more database tables.

SELECT name, email
FROM students;

The SELECT statement allows you to specify the columns that you want to retrieve.

3. Data Manipulation

SQL provides commands for adding, changing, and removing data.

INSERT INTO students (name)
VALUES ('Rahul');

UPDATE students
SET name = 'Amit'
WHERE id = 1;

DELETE FROM students
WHERE id = 2;

These operations are commonly referred to as data manipulation.

4. Database Definition

SQL includes statements for defining database objects such as tables.

CREATE TABLE students (
    id INT,
    name VARCHAR(100)
);

Commands such as CREATE, ALTER, and DROP are used to define or change database structures.

5. Powerful Filtering

The WHERE clause allows you to filter records according to specified conditions.

SELECT *
FROM students
WHERE age >= 18;

Only records satisfying the condition are returned.

6. Sorting Data

SQL can sort query results using ORDER BY.

SELECT *
FROM students
ORDER BY name ASC;

You can sort data in ascending or descending order.

ORDER BY name ASC;

ORDER BY name DESC;
7. Aggregate Functions

SQL provides aggregate functions for calculations on groups of rows.

Common aggregate functions include:

  • COUNT()
  • SUM()
  • AVG()
  • MIN()
  • MAX()
SELECT COUNT(*)
FROM students;
8. Grouping Data

The GROUP BY clause groups rows based on one or more columns.

SELECT course, COUNT(*)
FROM students
GROUP BY course;

This can be useful for generating summaries and reports.

9. Joining Tables

SQL can combine related information stored in different tables using joins.

SELECT students.name, courses.course_name
FROM students
INNER JOIN courses
ON students.course_id = courses.id;

Common joins include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN where supported.

10. Constraints

SQL constraints help enforce rules on data stored in tables.

Common constraints include:

  • PRIMARY KEY
  • FOREIGN KEY
  • UNIQUE
  • NOT NULL
  • CHECK
  • DEFAULT
CREATE TABLE students (
    id INT PRIMARY KEY,
    name VARCHAR(100) NOT NULL
);
11. Data Integrity

SQL constraints and relationships can help maintain the consistency and integrity of data.

For example, a foreign key can represent a relationship between a child table and a referenced parent table.

CREATE TABLE enrollments (
    student_id INT,
    course_id INT,
    FOREIGN KEY (student_id)
    REFERENCES students(id)
);
12. Subqueries

A subquery is a query written inside another SQL statement.

SELECT name
FROM students
WHERE age > (
    SELECT AVG(age)
    FROM students
);

Subqueries can be used to perform more advanced data retrieval.

13. Views

A view is a database object based on a query. It can provide a convenient way to access a selected set of data.

CREATE VIEW student_view AS
SELECT name, course
FROM students;

You can query the view like a table in supported database systems.

14. Indexes

Indexes can help a database find rows more efficiently for suitable queries.

CREATE INDEX idx_student_name
ON students(name);

Indexes can improve read performance, but they also require storage and can add overhead to data modifications.

15. Transactions

Transactions allow a group of database operations to be treated as a unit of work.

START TRANSACTION;

UPDATE accounts
SET balance = balance - 500
WHERE id = 1;

UPDATE accounts
SET balance = balance + 500
WHERE id = 2;

COMMIT;

Transaction behavior and exact commands can vary between database systems.

16. COMMIT and ROLLBACK

COMMIT makes the changes of a transaction permanent, while ROLLBACK can undo changes made during a transaction before they are committed, subject to the database system's transaction rules.

START TRANSACTION;

UPDATE students
SET course = 'Python'
WHERE id = 1;

ROLLBACK;
17. Security and Access Control

Database systems can provide mechanisms for controlling who can access databases and what operations they can perform.

Depending on the database system, SQL-related access-control statements can be used to grant or revoke privileges.

GRANT SELECT
ON students
TO some_user;

The exact syntax and privilege model depends on the database system.

18. Portability

SQL has standardized syntax that provides a common foundation across many relational database systems.

However, database products may support different features, functions, data types, and extensions.

Therefore, SQL code may sometimes need changes when moved between different database systems.

19. Works with Large Amounts of Data

Relational database systems can store and query large collections of structured data. SQL provides features for filtering, grouping, joining and aggregating that data.

SELECT course, COUNT(*) AS total_students
FROM students
GROUP BY course;

Database performance also depends on database design, indexes, queries, hardware, configuration and the specific database system.

20. SQL Works with Programming Languages

SQL is frequently used together with programming languages.

For example:

  • Python + SQL
  • PHP + SQL
  • Java + SQL
  • C# + SQL
  • JavaScript + SQL

The application sends database operations to a database system and then processes the returned results.

21. SQL Supports Reporting

SQL can be used to generate reports by combining filtering, aggregation, grouping and sorting.

SELECT course,
       COUNT(*) AS students,
       AVG(marks) AS average_marks
FROM students
GROUP BY course;

This can produce summarized information useful for applications and business reporting.

22. SQL Supports Different Data Operations
Operation Common SQL Statement
Create structure CREATE
Add data INSERT
Read data SELECT
Modify data UPDATE
Remove data DELETE
Change structure ALTER
Remove database objects DROP
23. Declarative Nature of SQL

SQL is primarily declarative. You generally describe the result or operation you want, while the database system determines how to execute the request.

SELECT name
FROM students
WHERE course = 'Python';

The query describes which rows and columns are required without specifying the database's internal execution steps.

24. SQL Supports Relationships

Relational databases can represent relationships between tables.

students
    |
    | student_id
    ↓
fees

Keys such as primary keys and foreign keys can be used to represent relationships between related tables.

25. SQL is Widely Used

SQL is an important skill for many technology roles involving databases and data.

  • Backend Developers
  • Database Developers
  • Data Analysts
  • Software Developers
  • Data Engineers
  • Database Administrators
Key Points
  • SQL provides a language for working with relational databases.
  • SQL can retrieve, insert, update and delete data.
  • SQL supports filtering and sorting.
  • Aggregate functions can calculate summaries.
  • GROUP BY can organize rows into groups.
  • JOINs can combine related data from multiple tables.
  • Constraints help enforce data rules.
  • Views provide query-based database objects.
  • Indexes can improve the performance of suitable queries.
  • Transactions help manage groups of database operations.
  • SQL is commonly used with programming languages and web applications.

🧠 Quick Quiz

Question: Which SQL clause is used to filter rows according to a condition?