Guida Dettagliata a SQL
SQL (Structured Query Language) è un linguaggio standard per la gestione e la manipolazione di database relazionali. Questa guida fornisce una panoramica delle principali funzionalità di SQL, insieme a risorse utili per approfondire.
Indice
- Introduzione a SQL
- Installazione di un Database
- Comandi di Base
- Filtri e Ordinamenti
- Join tra Tabelle
- Funzioni Aggregate
- Risorse Utili
Introduzione a SQL
SQL è utilizzato per comunicare con i database. Permette di eseguire operazioni come la creazione, lettura, aggiornamento e cancellazione di dati.
Risorse
Installazione di un Database
Per iniziare a utilizzare SQL, è necessario installare un sistema di gestione di database (DBMS). Alcuni dei più popolari includono:
Comandi di Base
SELECT
Il comando SELECT
è utilizzato per recuperare dati da una tabella.
SELECT column1, column2 FROM table_name;
SELECT column1, column2 FROM table_name;
INSERT
Il comando INSERT
è utilizzato per aggiungere nuovi record a una tabella.
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
UPDATE
Il comando UPDATE
è utilizzato per modificare i record esistenti in una tabella.
UPDATE table_name SET column1 = value1 WHERE condition;
UPDATE table_name SET column1 = value1 WHERE condition;
DELETE
Il comando DELETE
è utilizzato per rimuovere record da una tabella.
DELETE FROM table_name WHERE condition;
DELETE FROM table_name WHERE condition;
Filtri e Ordinamenti
WHERE
Il comando WHERE
è utilizzato per filtrare i risultati.
SELECT column1 FROM table_name WHERE condition;
SELECT column1 FROM table_name WHERE condition;
ORDER BY
Il comando ORDER BY
è utilizzato per ordinare i risultati.
SELECT column1 FROM table_name ORDER BY column2 ASC|DESC;
SELECT column1 FROM table_name ORDER BY column2 ASC|DESC;
GROUP BY
Il comando GROUP BY
è utilizzato per raggruppare i risultati.
SELECT column1, COUNT(*) FROM table_name GROUP BY column1;
SELECT column1, COUNT(*) FROM table_name GROUP BY column1;
Join tra Tabelle
Le join sono utilizzate per combinare righe da due o più tabelle.
SELECT columns FROM table1 JOIN table2 ON table1.column = table2.column;
SELECT columns FROM table1 JOIN table2 ON table1.column = table2.column;
Tipi di Join
- INNER JOIN: restituisce solo le righe che hanno corrispondenze in entrambe le tabelle.
- LEFT JOIN: restituisce tutte le righe dalla tabella di sinistra e le righe corrispondenti dalla tabella di destra.
- RIGHT JOIN: restituisce tutte le righe dalla tabella di destra e le righe corrispondenti dalla tabella di sinistra.
- FULL JOIN: restituisce righe quando c'è una corrispondenza in una delle tabelle.
Funzioni Aggregate
Le funzioni aggregate eseguono calcoli su un insieme di valori e restituiscono un singolo valore.
COUNT()
: conta il numero di righe.SUM()
: calcola la somma di una colonna.AVG()
: calcola la media di una colonna.MAX()
: restituisce il valore massimo.MIN()
: restituisce il valore minimo.
Risorse Utili
English version
SQL Detailed Guide
SQL (Structured Query Language) is a standard language for managing and manipulating relational databases. This guide provides an overview of the main features of SQL, along with useful resources for further study.
Table of Contents
Introduction to SQL
SQL is used to communicate with databases. It allows you to perform operations such as creating, reading, updating, and deleting data.
Resources
Installing a Database
To get started with SQL, you need to install a database management system (DBMS). Some of the most popular include:
Basic Commands
SELECT
The SELECT
command is used to retrieve data from a table.
SELECT column1, column2 FROM table_name;
SELECT column1, column2 FROM table_name;
INSERT
The INSERT
command is used to add new records to a table.
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
UPDATE
The UPDATE
command is used to modify existing records in a table.
UPDATE table_name SET column1 = value1 WHERE condition;
UPDATE table_name SET column1 = value1 WHERE condition;
DELETE
The DELETE
command is used to remove records from a table.
DELETE FROM table_name WHERE condition;
DELETE FROM table_name WHERE condition;
Filters and Sorts
WHERE
The WHERE
command is used to filter the results.
SELECT column1 FROM table_name WHERE condition;
SELECT column1 FROM table_name WHERE condition;
ORDER BY
The ORDER BY
command is used to sort the results.
SELECT column1 FROM table_name ORDER BY column2 ASC|DESC;
SELECT column1 FROM table_name ORDER BY column2 ASC|DESC;
GROUP BY
The GROUP BY
command is used to group the results.
SELECT column1, COUNT(*) FROM table_name GROUP BY column1;
SELECT column1, COUNT(*) FROM table_name GROUP BY column1;
Joins Between Tables
Joins are used to combine rows from two or more tables.
SELECT columns FROM table1 JOIN table2 ON table1.column = table2.column;
SELECT columns FROM table1 JOIN table2 ON table1.column = table2.column;
Join Types
- INNER JOIN: Returns only the rows that have matches in both tables.
- LEFT JOIN: Returns all the rows from the left table and the corresponding rows from the right table.
- RIGHT JOIN: Returns all rows from the right table and matching rows from the left table.
- FULL JOIN: Returns rows when there is a match in one of the tables.
Aggregate Functions
Aggregate functions perform calculations on a set of values ​​and return a single value.
COUNT()
: Counts the number of rows.SUM()
: Calculates the sum of a column.AVG()
: Calculates the average of a column.MAX()
: Returns the maximum value.MIN()
: Returns the minimum value.
Commenti
Posta un commento