For a very long time, SQL was reserved for knowledgeable and technical people in the IT department, and not just any company entity or department could do it. It used to be the exclusive preserve of the company’s IT department. Now, with the spread of « IT », many departments are able to access their company’s data using SQL to query their databases, including marketing, accounting, management control, human resources and many others!

Are you a company specialising in e-commerce, healthcare, retail or simply an SME / SMI? Do you have a set of data stored in a database?

It’s essential to know the basics of structured query language (SQL) so that you can quickly get answers to your queries.

DEFINITION

SQL, or Structured Query Language, is a programming language specially designed for managing and manipulating relational databases.

It provides a standardised interface enabling users to communicate with databases and carry out operations such as inserting, updating, deleting and retrieving data efficiently.

THE BASICS OF SQL

Remember that SQL is nothing more than a way of reading the contents of a relational database to retrieve the information a user needs to meet a requirement.

DATA STRUCTURING

SQL is based on the relational model, which organises data in the form of tables. Each table is made up of columns (fields) representing specific attributes, and rows containing the records.

Table structure :

In the world of SQL, table structure is crucial. Each table is defined by columns, where each column represents a particular attribute of the data you are storing. For example, an « employees » table might have columns such as « surname« , « first name« , « age« , etc. These tables are linked by keys. These tables are linked by keys, which can be unique identifiers for each record, facilitating relationships between different tables.

The main operations (or commands / basic SQL queries)

SELECT : Used to extract data from one or more tables. The SELECT clause is used to specify the columns to be retrieved, the filter conditions and the sort order. This clause is one of the most fundamental in SQL. The WHERE clause, often used with SELECT, is used to filter the results according to specific conditions. For example, you might want to retrieve only those employees whose age is greater than 30, or as in the example below, only those employees in the sales department.

SELECT last name, first name FROM employees WHERE department = ‘Sales’;

INSERT: Used to add new rows to a table

INSERT INTO customers (last name, first name, email) VALUES (‘Doe’, ‘John’, ‘john.doe@email.com’);

UPDATE: Used to add new rows to a table

UPDATE products SET price = price * 1.1 WHERE category = ‘Electronics’;

DELETE: Used to delete rows from a table under certain conditions

DELETE FROM orders WHERE order_date < ‘2023-01-01‘;

Filtering and sorting

To filter the results, SQL uses the WHERE clause, which allows you to specify conditions for selecting the data. In addition, the ORDER BY clause is used to sort the results according to one or more columns.

Filtering and sorting are essential operations in the SQL language, making it possible to retrieve specific data and organise it in a meaningful way. Let’s explore these concepts with some practical examples

Filtering with the WHERE clause

The WHERE clause is used to filter the results of a query by specifying conditions. This allows you to select only the data that meets these criteria.

–Select employees with a salary greater than 50000

SELECT last name, first name, salary

FROM employees

WHERE salary > 50000;

In this example, only employees with a salary greater than 50000 will be included in the results.

Filtering with the ORDER BY clause

The ORDER BY clause is used to sort the results of a query according to one or more columns. You can specify the sort order (ascending or descending)

–Select customers and sort alphabetically by name

SELECT last name, first name, email

FROM customers

ORDER BY name ASC;

In this example, the results will be sorted in ascending alphabetical order by customer name.

Filtering and sorting can also be combined, i.e. combining the WHERE clause and the ORDER BY clause to filter the results at the same time

–Select products in the ‘Electronics’ category and sort by descending price

SELECT product_name, price

FROM products

WHERE category = ‘Electronics

ORDER BY price DESC;

There are other ways of filtering and sorting with operators, but this becomes SQL that is no longer basic, but for a more experienced audience.

By understanding these filtering and sorting concepts, you will be able to extract specific data from your SQL databases in a targeted and organised way.

Joins

Joins are essential for combining data from several tables.

Common types of joins include INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN, each offering specific methods for associating rows between different tables.

Example of a simple join:

SELECT customer.name, orders.date

FROM customers

INNER JOIN orders ON customers.customer_id = orders.customer_id;

Types of joins :

INNER JOIN: Returns the rows when the join condition is true in both tables.

LEFT JOIN (or LEFT OUTER JOIN): Returns all the rows in the left-hand table and the corresponding rows in the right-hand table.

RIGHT JOIN (or RIGHT OUTER JOIN): The opposite of LEFT JOIN.

FULL JOIN (or FULL OUTER JOIN): Returns all rows when the join condition is true in one of the two tables.

Constraints for data integrity and Indexes to optimise performance

Constraints play a crucial role in guaranteeing data integrity. Primary keys ensure that each record in a table is unique, while foreign keys establish links between different tables. Uniqueness constraints ensure that no duplicate values are allowed in a specified column.

Indexes are data structures that improve query performance by speeding up data searches. Creating an index on a column makes searching easier, but it is essential to use them wisely, as they can also increase the size of the database.

Conclusion

SQL is a powerful and universal tool for working with relational databases. Understanding its fundamentals enables developers and data analysts to interact effectively with database management systems, making it easier to manipulate and retrieve crucial information. Whether for simple tasks or more complex operations, SQL remains an essential part of data management.

It offers a range of tools for interacting with relational databases in a powerful and flexible way. By understanding these basic concepts, you’ll be better equipped to effectively manipulate data, create custom reports and answer complex questions from large datasets. Whether you’re a developer, data analyst or database administrator, mastering SQL is an invaluable asset in the world of data management.

Did this article inspire you ?