ITclub

العودة إلى Database
محاضرة 5

Introduction to SQL: Basic Queries (DQL)

مقدمة لغة SQL، أقسامها (DQL, DDL, DML)، وأوامر الاستعلام SELECT و WHERE.

ملخص المحاضرة

Lecture 5: Introduction to SQL - Data Query Language

1. What is SQL?

SQL (Structured Query Language) is the standard language used to communicate with, manage, and manipulate relational databases. It works across various systems (Access, MySQL, SQL Server).

2. SQL Categories

  1. DQL (Data Query Language): Used to retrieve data (SELECT).
  2. DDL (Data Definition Language): Used to define structure (CREATE, DROP, ALTER).
  3. DML (Data Manipulation Language): Used to modify data (INSERT, UPDATE, DELETE).

3. The SELECT Statement (DQL)

The SELECT statement is the most used command. It fetches data from the database.

  • Syntax:
    SELECT column1, column2 FROM table_name;
  • Select All: Using the asterisk (*) retrieves all columns.
    SELECT * FROM Students;
  • SELECT DISTINCT: Retrieves unique values only (removes duplicates).
    SELECT DISTINCT City FROM Customers;

4. Filtering Data: The WHERE Clause

Used to filter records that meet a specific condition.

  • Syntax:
    SELECT Name, Age FROM Students WHERE Age > 20;
  • Comparison Operators:
    • =: Equal to
    • > / <: Greater than / Less than
    • >= / <=: Greater/Less than or equal
    • <> or !=: Not equal
  • Logical Operators:
    • AND: Both conditions must be true.
    • OR: At least one condition must be true.
    • NOT: Reverses the condition.

5. Sorting Data: ORDER BY

Used to sort the result set.

  • ASC: Ascending (Default).
  • DESC: Descending.
    SELECT * FROM Employees ORDER BY Salary DESC;