SQL is the key to unlocking data in databases. It lets you grab specific info, filter results, and combine data from different tables. With SQL, you can pull out exactly what you need and organize it just how you want.
Knowing SQL basics gives you the power to dig into databases and find stories. You'll learn how to retrieve, sort, and manipulate data, making it easier to spot trends and uncover insights for your reporting.
Basic SQL Queries
Retrieving Data with SELECT
- SQL (Structured Query Language) provides a standard way to store, manipulate, and retrieve data from relational databases
- The
SELECTstatement retrieves data from one or more tables, allowing you to specify columns, tables, and optional filtering conditions - The
FROMclause specifies the table(s) from which data should be retrieved, followed by the table name(s)
Filtering and Sorting Results
- The
WHEREclause filters results based on specified conditions, retrieving only rows that meet certain criteria using comparison operators (=,<,>,<=,>=) and logical operators (AND,OR,NOT) - The
ORDER BYclause sorts retrieved data based on one or more columns, specifying the column(s) and sorting order (ASCfor ascending,DESCfor descending) - The
LIMITclause restricts the number of rows returned by the query, useful for pagination or retrieving a subset of results (top 10 rows)
Refining Query Results
Selecting Columns and Joining Tables
- The
SELECTclause specifies columns to retrieve using the asterisk (*) for all columns or individual column names separated by commas - The
FROMclause specifies the table(s) for data retrieval, separating multiple tables with commas - Clauses can be combined in a single
SELECTstatement to refine query results according to specific requirements (filtering, sorting, limiting)
Aggregating and Grouping Data
- Aggregate functions (
COUNT,SUM,AVG,MAX,MIN) calculate summary values across multiple rows, providing insights into data - The
GROUP BYclause groups rows based on specified columns, often used with aggregate functions to calculate values for each group - The
HAVINGclause filters grouped results based on conditions, similar to theWHEREclause but applied after grouping
Data Manipulation with SQL
Inserting and Updating Data
- The
INSERTstatement inserts new rows into a table, specifying the table name and values for each column - The
INSERT INTOclause is followed by the table name and optional column names, with values provided in the same order if column names are omitted - The
UPDATEstatement modifies existing data in a table, updating specific columns of one or more rows based on conditions - The
SETclause specifies columns to be updated and their new values, separated by commas - The
WHEREclause determines which rows should be updated based on specified conditions
Deleting Data
- The
DELETEstatement removes one or more rows from a table based on specified conditions - The
FROMclause specifies the table from which rows should be deleted - The
WHEREclause determines which rows should be deleted based on conditions, ensuring only intended rows are removed
Combining Data with JOINs
Types of JOINs
INNER JOIN(orJOIN) returns only rows with matching values in both tables, combining data based on a related columnLEFT JOIN(orLEFT OUTER JOIN) returns all rows from the left table and matched rows from the right table, usingNULLfor non-matching columnsRIGHT JOIN(orRIGHT OUTER JOIN) returns all rows from the right table and matched rows from the left table, usingNULLfor non-matching columnsFULL OUTER JOINreturns all rows from both tables, including matched and unmatched rows, usingNULLfor non-matching columns
Specifying Join Conditions
- The
ONclause specifies the condition that determines how tables are related, defining the column(s) on which tables should be joined - The
USINGclause simplifies theJOINsyntax when the join condition involves columns with the same name in both tables, specifying the common column name