# What is the Structured Query Language (famous SQL)?
Structured Query Language (SQL) is a programming language used to manage and manipulate data stored in relational databases. It is the standard language for working with relational databases and is used to create, query, and modify the data stored in the database.
SQL is a declarative language, which means that it is used to describe what needs to be done, rather than specifying how it should be done. This allows the database management system (DBMS) to optimize the execution of SQL queries and ensure efficient use of system resources.
SQL is used to perform a wide range of tasks, including creating and modifying tables and indices, inserting and updating data, and retrieving data from the database. It is also used to define the structure and relationships of the data within the database, as well as to set permissions and controls for users.
SQL is a powerful and versatile language that is widely used in a variety of industries, including business, finance, healthcare, and government. It is an essential tool for database administrators and developers, and is also used by data analysts and data scientists to extract, transform, and load data for analysis and reporting purposes.
There are several dialects of SQL, including MySQL, Oracle, and Microsoft SQL Server, each with its own unique features and syntax. However, the core concepts and principles of SQL are the same across all dialects, making it a language that is easy to learn and widely applicable.
# Examples of SQL Queries
Here are some examples of SQL queries that can be run on the SQL Server Database:
## _Select Query_
- The following query selects all fields from the "Customers" table where the "City" field is equal to "São Paulo":
```sql
SELECT * FROM Customers WHERE City = 'São Paulo';
```
## _Insert Data_
- The following query inserts a new record into the "Customers" table:
```sql
INSERT INTO Customers (Name, Address, City) VALUES ('João da Silva', 'Rua das Flores, 123', 'São Paulo');
```
## _Update Data_
- The following query updates the "Address" field of the record with ID 1 in the "Customers" table:
```sql
UPDATE Customers SET Address = 'Rua das Oliveiras, 456' WHERE ID = 1;
```
## _Delete Data_
- The following query deletes the record with ID 1 from the "Customers" table:
```sql
DELETE FROM Customers WHERE ID = 1;
```
# Two simple examples
## First example
> You are the database administrator for a small customer database. You need to add a new customer record with the following information:
- Name: "John Smith"
- Address: "123 Main Street"
- City: "New York"
## _Solution_
- To add the new customer record, you can use the following SQL query:
```sql
INSERT INTO Customers (Name, Address, City) VALUES ('John Smith', '123 Main Street', 'New York');
```
_This query will insert a new record into the "Customers" table with the specified name, address, and city._
## Second example
> You are the database administrator for a product inventory database. You need to update the quantity of a product with the ID "12345" to 10.
## _Solution_
- You are the database administrator for a product inventory database. You need to update the quantity of a product with the ID "12345" to 10.
```sql
UPDATE Products SET Quantity = 10 WHERE ID = 12345;
```
_This query will update the "Quantity" field of the product with the ID "12345" to the value 10._
# Complex example
Here is a complex example and solution involving the SQL Server Database:
> You are the database administrator for a large online retailer. The company has a database with several tables storing information about customers, orders, and products. The database is used to track customer information, process orders, and generate reports.
> One of the company's sales managers has requested a report showing the total sales and number of orders for each customer, broken down by product category. The report should show the data for the past year, and should be sorted by total sales in descending order.
## _Solution_
- To generate the requested report, you can use the following SQL query:
```sql
SELECT c.Name AS 'Customer',
SUM(o.Total) AS 'Total Sales',
COUNT(o.ID) AS 'Number of Orders',
p.Category AS 'Product Category'
FROM Customers c
INNER JOIN Orders o ON c.ID = o.CustomerID
INNER JOIN Products p ON o.ProductID = p.ID
WHERE o.Date BETWEEN '2022-01-01' AND '2022-12-31'
GROUP BY c.Name, p.Category
ORDER BY 'Total Sales' DESC;
```
_This query will select the customer name, total sales, number of orders, and product category for all orders placed within the specified date range. It will then group the results by customer name and product category, and will sort the results by total sales in descending order. The resulting report will show the total sales and number of orders for each customer, broken down by product category._
> I hope the examples have helped you.
###### _Here are some additional reference links that you might find helpful in learning more about SQL Server Database and the SQL language:_
- The SQL Server homepage: https://www.microsoft.com/sql-server/
- The SQL Server documentation: https://docs.microsoft.com/sql/
- The SQL tutorial: https://www.w3schools.com/sql/
- The MySQL reference guide: https://dev.mysql.com/doc/refman/8.0/en/
- The Oracle SQL tutorial: https://docs.oracle.com/en/database/oracle/oracle-database/19/lnpls/index.html
> I hope these additional references and syntax are helpful to you.