SQL Basics for Beginners: A Comprehensive Guide

Lahari T
3 min readApr 4, 2024

--

Introduction:

SQL (Structured Query Language) is a powerful tool used for managing and manipulating relational databases. In this article, we will cover the fundamental concepts of SQL, including querying data, filtering results, Concatenation of strings, and basic CRUD operations (Create, Read, Update, Delete).

By the end of this guide, you will have a solid understanding of SQL basics and be ready to start writing your own SQL queries.

Here’s a sample Employees table with 5 records:

Table 1: Example Employees Table

This table includes columns for EmployeeID, FirstName, LastName, Position, Department, and Salary, with 5 sample records of employees.

  1. Querying Data:
  • The SELECT statement is used to retrieve data from a database table.
  • Syntax: SELECT column1, column2 FROM table_name;
    Example:
    SELECT first_name, last_name FROM employees;
Table 2 : Retrieving the first_name and last_name columns from the employees table.

2. Filtering Results:

  • The WHERE clause is used to filter records based on specified conditions.
  • The ‘*’ operator is used when we want to retrieve all the columns from a given table.
  • Syntax: SELECT * FROM table_name WHERE condition;`
    Example:

    SELECT * FROM employees WHERE Department= ‘Analytics’;
Table 3: Retrieving all the records whose Department is “Analytics”

3. String Concatenation:

  • SQL allows you to concatenate strings together using the CONCAT() function or the || operator.
  • ‘As’ is alias name for any column’s original name.
  • Syntax: CONCAT(string1, string2, …)

SELECT CONCAT(first_name, ‘ ‘, last_name) AS full_name FROM employees;

Table 4: full_name column after concatenation of first and last names

4. Basic CRUD Operations:

Create (INSERT): Used to add new records to a table.

  • Syntax: INSERT INTO table_name (column1, column2) VALUES (value1, value2);
  • Example:
    INSERT INTO employees (first_name, last_name)
    VALUES
    (‘Michael’, ‘Johnson’),
    (‘Samantha’, ‘Taylor’);
Table 5: Updated Employeetable with two new records

Read (SELECT): Covered earlier, used to retrieve records/data from a table.

Update (UPDATE): Used to modify existing records in a table.

  • Syntax: UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;

Example:

UPDATE employees SET salary = 50000 WHERE department = ‘Sales’;

Table 6: Updated table after setting new salary

Delete (DELETE): Used to remove records from a table.

  • Syntax: DELETE FROM table_name WHERE condition;
    Example:

    DELETE FROM customers WHERE EmployeeID= 5;
Table 7: New Employees table with deleted row

Conclusion:

SQL is a fundamental skill for anyone working with relational databases. In this article, we covered the basics of SQL, including querying data, filtering results, string concatenation, and basic CRUD operations. With this knowledge, you can start writing your own SQL queries and manipulating data in databases. Keep practicing and exploring more advanced SQL concepts to become proficient in database management.

--

--