Guides and tutorials

Hundreds of tutorials and step by step guides carefully written by our support team.

Basic MySQL queries

MySQL is a widely used relational database management system that allows you to store and retrieve data efficiently. Here are the basic MySQL queries so that you can interact with your database using, as an example, a database with employees.

Connecting to the Database

Before performing any query, you must connect to the database. Make sure you have the login credentials (username, password, database name) before executing this step.

mysql -u usuario -p contraseña -h host -D nombre_base_de_datos

You also have the option to connect using a program such as HeidiSQL. If you want more information about this program and how to install it, you can follow THIS MANUAL.

Creating a Table

To store data, you must first create a table in the database. A table is a structure that defines the data schema you want to store.

CREATE TABLE nombre_tabla (
  columna1 tipo_de_dato1,
  columna2 tipo_de_dato2,
  ...
);

Example:

CREATE TABLE empleados (
  id INT AUTO_INCREMENT PRIMARY KEY,
  nombre VARCHAR(50),
  edad INT,
  salario FLOAT
);

Insert Data into the Table

Once the table is created, you can add data using the INSERT INTO statement.

INSERT INTO nombre_tabla (columna1, columna2, ...) VALUES (valor1, valor2, ...);

Example:

INSERT INTO empleados (nombre, edad, salario) VALUES ('Juan Pérez', 30, 2500.00);
INSERT INTO empleados (nombre, edad, salario) VALUES ('María Gómez', 25, 2200.50);
INSERT INTO empleados (nombre, edad, salario) VALUES ('Pedro Ramirez', 35, 2800.75);

Query Data

To retrieve data from the table, you can use the SELECT statement.

SELECT columna1, columna2, ... FROM nombre_tabla;

In case you want to retrieve all the data in the table, you can use the following query.

SELECT * FROM nombre_tabla;

Example:

SELECT nombre, salario FROM empleados;

Filter Data with WHERE

If you want to get records that meet certain conditions, you can use the WHERE clause.

SELECT columna1, columna2, ... FROM nombre_tabla WHERE condicion;

Example:

SELECT * FROM empleados WHERE edad > 28;

Update Data

To modify existing data in a table, use the UPDATE statement together with the WHERE clause to specify which records to update.

UPDATE nombre_tabla SET columna1 = valor1, columna2 = valor2, ... WHERE condicion;

Example:

UPDATE empleados SET salario = 3000.00 WHERE id = 1;

Deleting Data

To delete records from a table, use the DELETE statement together with the WHERE clause to specify which records to delete.

DELETE FROM nombre_tabla WHERE condicion;

Example:

DELETE FROM empleados WHERE edad < 25;

Limit Results

If you want to get only a specific number of records, you can use the LIMIT clause.

SELECT columna1, columna2, ... FROM nombre_tabla LIMIT cantidad;

Example:

SELECT * FROM empleados LIMIT 2;

Sort Results

To sort the results according to one or more columns, use the ORDER BY clause.

SELECT columna1, columna2, ... FROM nombre_tabla ORDER BY columna1 ASC/DESC, columna2 ASC/DESC, ...;

Example:

SELECT * FROM empleados ORDER BY salario DESC;

Group Results

If you want to perform aggregate operations, such as addition or counting, you can use the GROUP BY clause.

SELECT columna1, SUM(columna2) as suma_columna2 FROM nombre_tabla GROUP BY columna1;

Example:

SELECT edad, COUNT(*) as cantidad FROM empleados GROUP BY edad;

These are some of the basic queries that will allow you to start interacting with MySQL. As you become more familiar with the SQL language, you will be able to perform more complex queries and take full advantage of MySQL's potential to manage your data