You are currently viewing SQL :1.1 Introduction to SQL

SQL :1.1 Introduction to SQL

Introduction to SQL

What is SQL?

SQL (Structured Query Language) is a domain-specific language used in programming and designed for managing data held in a relational database management system (RDBMS). It allows you to query, manipulate, and manage data in databases from HTML. In this tutorial, we’ll cover the basics of SQL, including:

  1. Basic Syntax
  2. Creating Databases and Tables
  3. Inserting Data
  4. Querying Data
  5. Updating and Deleting Data

1. Basic Syntax

SQL commands are written in uppercase, but it’s not required. However, it’s a convention to make SQL commands stand out.

Sample Code:

SELECT column1, column2 FROM table_name WHERE condition;
  • SELECT: Used to select data from a database
  • column1, column2: Columns to be retrieved
  • FROM: Indicates which table to select from
  • table_name: Name of the table
  • WHERE: Filters the rows to be selected based on a condition

Example:

Let’s say we have a table named employees with columns id, name, and age.

SELECT name, age FROM employees WHERE age > 30;

This query will select the name and age of employees who are older than 30.

2. Creating Databases and Tables

To create a new database, we use the CREATE DATABASE command. To create a table within a database, we use the CREATE TABLE command.

Sample Code:

CREATE DATABASE company_db;
USE company_db;

CREATE TABLE employees (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50),
    age INT,
    salary DECIMAL(10, 2)
);
  • CREATE DATABASE: Creates a new database
  • USE: Specifies the database to work with
  • CREATE TABLE: Creates a new table
  • AUTO_INCREMENT: Automatically increments the value of the field
  • PRIMARY KEY: Uniquely identifies each record
  • VARCHAR: Variable-length character string
  • INT: Integer
  • DECIMAL: Decimal number

Example:

This code creates a database named company_db and a table named employees with columns id, name, age, and salary.

3. Inserting Data

To add data into a table, we use the INSERT INTO command.

Sample Code:

INSERT INTO employees (name, age, salary) VALUES ('John Doe', 35, 50000);
  • INSERT INTO: Adds a new record into the table
  • VALUES: Specifies the values to be inserted

Example:

This code inserts a new employee named John Doe, aged 35, with a salary of 50000 into the employees table.

4. Querying Data

To retrieve data from a table, we use the SELECT command.

Sample Code:

SELECT * FROM employees;
  • *: Selects all columns

Example:

This code retrieves all records from the employees table.

5. Updating and Deleting Data

To update existing data, we use the UPDATE command. To delete data, we use the DELETE command.

Sample Code:

UPDATE employees SET salary = 60000 WHERE name = 'John Doe';
  • UPDATE: Modifies existing records
  • SET: Specifies the columns to be updated
DELETE FROM employees WHERE age > 40;
  • DELETE FROM: Removes records from a table

Example:

The first code updates John Doe’s salary to 60000. The second code deletes employees older than 40.

Conclusion

SQL is a powerful language for working with databases. This tutorial covered the basics including syntax, creating databases and tables, inserting, querying, updating, and deleting data. Practice these concepts on your own to solidify your understanding of SQL.

Leave a Reply