Fundamentos de Bases de datos
Cómo diseñar bases de datos y hacer queries efectivos
Cómo identificar y organizar datos en bases de datos
Introducción práctica a SQL y bases de datos relacionales
Trabajo Directo con MySQL: Cliente, Servidor y Manejo de Errores
Instalación de MySQL en varios sistemas operativos
Quiz: Fundamentos de Bases de datos
Introducción a Bases de Datos Relacionales
Conceptos básicos de bases de datos: columnas, tuplas y relaciones
Creación de Tablas en MySQL con Tipos de Datos Esenciales
Creación de tablas en MySQL con Primary Key y Timestamp
Normalización y relaciones en bases de datos relacionales
Creación y visualización de bases de datos con DBML y DBDiagram
Quiz: Introducción a Bases de Datos Relacionales
Manipulación de Datos
Cómo crear tablas y utilizar 'Foreign Keys' en MySQL
Crear tablas y establecer llaves foráneas en SQL
Tipos de tablas en una base de datos relacional
Cómo Modificar Tablas en SQL con ALTER TABLE
Cómo Insertar Datos y Manejar Errores en MySQL
Creación Avanzada de una Tabla Products en MySQL
Uso práctico del WHERE en SQL para filtrar datos con precisión
Guía práctica para modificar datos usando UPDATE en SQL
Cómo eliminar datos en SQL: métodos lógicos y físicos
Uso Avanzado del Comando SELECT en SQL
Quiz: Manipulación de Datos
Agrupación de Datos
Funciones agregadoras en MySQL para análisis eficiente de datos
Insertar datos desde otra tabla en MySQL con Insert Into Select
Cómo utilizar Left Join en bases de datos relacionales
Cómo consultar y relacionar tablas en MySQL
You don't have access to this class
Keep learning! Join and start boosting your career
Are you starting with databases and want to know how to query useful information from various tables in MySQL? Learning how to relate tables correctly allows you to get valuable information and make better decisions based on real data.
When working with relational databases in MySQL, tables are fundamental. In this example there are four key tables:
Each table has specific relationships that make it easy to extract specific information.
Building SQL queries using joins allows you to gather relevant data from multiple tables. Here's how to do it step by step:
You start a basic query with the main table:
SELECT bill.billID, bill.status, clients.nameFROM bills AS billLEFT JOIN clients AS clients ON bill.clientID = clients.clientID;
This type of query returns essential data: account number, status and name of the linked customer.
The query can be extended to count how many products were purchased on each invoice:
SELECT bill.billID, bill.status, clients.name, COUNT(bp.billproductID) AS number_of_productsFROM bills AS billLEFT JOIN clients AS clients ON bill.clientID = clients.clientIDLEFT JOIN billproducts AS bp ON bp.billID = bill.billIDGROUP BY bill.billID;
This modification uses count() to know exactly how many products each bill has.
To have a deeper detail and consider discounts, another join is added to the products table to get current prices:
SELECT bill.billID, bill.status, clients.name, COUNT(bp.billproductID) AS number_of_products, ROUND(SUM(bp.quantity * product.price * (1 - bp.discount / 100)), 2) AS totalFROM bills AS billLEFT JOIN customers AS customers ON bill.clientID = clients.clientIDLEFT JOIN billproducts AS bp ON bp.billID = bill.billIDLEFT JOIN products AS product ON bp.productID = product.productIDGROUP BY bill.billID;
With this query, you automatically get a detailed summary of purchases with the total amount including discounts applied.
If you want your report to be easily readable, concatenating values into text is an excellent option:
SELECT CONCAT(clients.name, ' has an account ', bill.status, ' with ', COUNT(bp.billproductID), ' products and sum ', ROUND(SUM(bp.quantity * product.price * (1 - bp.discount / 100))), 2)) AS resultFROM bills AS billLEFT JOIN clients AS clients ON bill.clientID = clients.clientIDLEFT JOIN billproducts AS bp ON bp.billID = bill.billID LEFT JOIN products AS product ON bp.productID = product.productIDGROUP BY bill.billID;
This generates clear sentences that easily summarize business information.
Understanding databases and the right way to leverage their potential greatly helps developers and data scientists to derive real value, make informed decisions and define sound, evidence-based strategies. A carefully structured database not only facilitates quick access to accurate information, it also provides flexibility and sustainable growth in the future.
Did you find this guide to SQL queries useful? Share your ideas and experiences about databases in the comments!
Contributions 2
Questions 0
Want to see more contributions, questions and answers from the community?