SQL basics: How to start your adventure with databases

16 July 2024

EN SQL basics_ How to start your adventure with databases

Learn how to get started with SQL and databases. A beginner’s guide.

SQL (Structured Query Language) is a language that plays a crucial role in managing and manipulating databases. With SQL, we can create, modify, delete, and query databases, making it an essential tool for anyone working with data. In this article, I will introduce the basics of SQL to help you start your journey with databases. I will focus on the most important commands and show you how to use them with concrete examples.

Introduction

Starting with SQL might seem daunting, but understanding the basic concepts and commands can quickly boost your confidence. SQL is a declarative language, which means you describe what you want to get rather than how to do it. This allows you to focus on the data rather than the complexity of the algorithms.

Creating a Database and Tables

Creating a Database

The first step in working with SQL is to create a database. We use the CREATE DATABASE command for this.

CREATE DATABASE my_database;

Creating a Table

Next, we need to create a table to store our data. The CREATE TABLE command allows us to define the structure of the table.

CREATE TABLE employees (

    id INT PRIMARY KEY,

    first_name VARCHAR(50),

    last_name VARCHAR(50),

    position VARCHAR(50),

    salary DECIMAL(10, 2)

);

Inserting Data

To add data to our table, we use the INSERT INTO command.

INSERT INTO employees (id, first_name, last_name, position, salary)

VALUES (1, 'John', 'Doe', 'Developer', 7000.00);

Querying the Database

Selecting Data

The most commonly used command in SQL is SELECT, which allows you to select data from a table.

SELECT first_name, last_name FROM employees;

Filtering Data

To filter data, we use the WHERE clause.

SELECT first_name, last_name FROM employees WHERE position = 'Developer';

Sorting Data

The ORDER BY command allows you to sort the results of a query.

SELECT first_name, last_name, salary FROM employees ORDER BY salary DESC;

Aggregating Data

SQL also enables aggregating data, such as calculating the average salary. We use aggregate functions like AVG, SUM, COUNT, MAX, and MIN.

SELECT AVG(salary) AS average_salary FROM employees;

Modifying Data

Updating Data

To update data in a table, we use the UPDATE command.

UPDATE employees SET salary = 7500.00 WHERE id = 1;

Deleting Data

To delete data from a table, we use the DELETE command.

DELETE FROM employees WHERE id = 1;

Basic Operations on Tables

Adding a Column

If we want to add a new column to an existing table, we use the ALTER TABLE command.

ALTER TABLE employees ADD age INT;

Dropping a Column

To drop a column from a table, we also use ALTER TABLE.

ALTER TABLE employees DROP COLUMN age;

List of Basic SQL Commands

  1. CREATE DATABASE: Creates a new database.
  2. CREATE TABLE: Creates a new table.
  3. INSERT INTO: Inserts new data into a table.
  4. SELECT: Selects data from a table.
  5. WHERE: Filters data.
  6. ORDER BY: Sorts query results.
  7. UPDATE: Updates existing data in a table.
  8. DELETE: Deletes data from a table.
  9. ALTER TABLE: Modifies the structure of a table.
  10. DROP TABLE: Deletes a table from the database.

Conclusion

The basics of SQL are essential for anyone wanting to work with databases. By understanding and mastering the basic commands, we can effectively manage data, perform queries, and modify database structures. I hope this article helped you understand how to start your journey with SQL and that databases are no longer a mystery to you. I encourage you to further explore this topic and experiment with different queries.

Other interesting articles:

Prefer to read in Polish? No problem!

That’s all on this topic. Analyze in peace!

Did you like this article 🙂?
Share it on Social Media 📱
>>> You can share it on LinkedIn and show that you learn something new every day.
>>> You can throw it on Facebook – and perhaps help a friend of yours who is looking for this.
>>> And remember to bookmark this page, you never know if it won’t come handy in in the future.

You prefer to watch 📺 – no problem
>>> Subscribe and watch my English channel on YouTube.

Ja Ci ją z przyjemnością wyślę. Za darmo. Bez spamu.

Poradnik Początkującego Analityka

Video - jak szukać pracy w IT

Regularne dawki darmowej wiedzy, bez spamu.