SQL Basics
GHOUSSEIN Edouardo
October 23, 2024
1 Introduction
SQL (Structured Query Language) is a standardized language for managing and
manipulating databases. This document provides a comprehensive overview of
basic SQL commands with examples.
2 Basic SQL Commands
2.1 1. Creating a Database
Command: CREATE DATABASE
Description: Creates a new database.
1 CREATE DATABASE mydatabase ;
Listing 1: Create Database Example
2.2 2. Creating a Table
Command: CREATE TABLE
Description: Creates a new table in the database.
1 CREATE TABLE employees (
2 id INT PRIMARY KEY ,
3 name VARCHAR (100) ,
4 position VARCHAR (50) ,
5 salary DECIMAL (10 , 2)
6 );
Listing 2: Create Table Example
2.3 3. Inserting Data
Command: INSERT INTO
Description: Inserts new records into a table.
1
1 INSERT INTO employees ( id , name , position , salary ) VALUES
2 (1 , ’ Alice ’ , ’ Manager ’ , 60000.00) ,
3 (2 , ’ Bob ’ , ’ Developer ’ , 50000.00) ;
Listing 3: Insert Data Example
2.4 4. Querying Data
Command: SELECT
Description: Retrieves data from a table.
1 SELECT * FROM employees ;
Listing 4: Select Data Example
2.5 5. Filtering Results
Command: WHERE
Description: Filters records based on specified conditions.
1 SELECT * FROM employees WHERE salary > 55000;
Listing 5: Where Clause Example
2.6 6. Updating Records
Command: UPDATE
Description: Modifies existing records in a table.
1 UPDATE employees SET salary = salary * 1.10 WHERE position = ’
Developer ’;
Listing 6: Update Data Example
2.7 7. Deleting Records
Command: DELETE
Description: Removes records from a table.
1 DELETE FROM employees WHERE id = 1;
Listing 7: Delete Data Example
2
2.8 8. Joining Tables
Command: JOIN
Description: Combines rows from two or more tables based on a related
column.
1 SELECT employees . name , departments . name
2 FROM employees
3 JOIN departments ON employees . department_id = departments . id ;
Listing 8: Join Tables Example
2.9 9. Grouping Results
Command: GROUP BY
Description: Groups rows that have the same values in specified columns.
1 SELECT position , COUNT (*) AS count
2 FROM employees
3 GROUP BY position ;
Listing 9: Group By Example
2.10 10. Ordering Results
Command: ORDER BY
Description: Sorts the result set in ascending or descending order.
1 SELECT * FROM employees ORDER BY salary DESC ;
Listing 10: Order By Example
2.11 11. Aggregating Data
Command: COUNT, SUM, AVG, MIN, MAX
Description: Performs calculations on a set of values.
1 SELECT AVG ( salary ) AS averag e_salary FROM employees ;
Listing 11: Aggregate Functions Example
3 Conclusion
This document covers the basic SQL commands and their usage. For more
advanced topics, refer to the official SQL documentation or additional resources.