parent
169a8f0356
commit
72f4faaf67
134 changed files with 5703 additions and 8374 deletions
@ -1,54 +0,0 @@ |
|||||||
# SELECT |
|
||||||
|
|
||||||
The `SELECT` statement is used in SQL to pick out specific data from a database. In other words, it is used to select from the database what you would like to display. The syntax for the `SELECT` statement is fairly straightforward: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT column(s) |
|
||||||
FROM table |
|
||||||
WHERE condition; |
|
||||||
``` |
|
||||||
|
|
||||||
- `column(s)`: Enter the name(s) of the column(s) that you want to display. |
|
||||||
- `table`: The name of the table from where you want to retrieve data. |
|
||||||
- `WHERE`: Optional. This is a filter to display only the rows where this condition is true. |
|
||||||
|
|
||||||
For instance, if you wanted to select all data from the "Customers" table, your query would look like this: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT * |
|
||||||
FROM Customers; |
|
||||||
``` |
|
||||||
In the above code, the asterisk `*` denotes "all". This will retrieve all of the data in the "Customers" table. |
|
||||||
|
|
||||||
If you want to select only the "FirstName" and "LastName" columns from the "Customers" table, your query would look like this: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT FirstName, LastName |
|
||||||
FROM Customers; |
|
||||||
``` |
|
||||||
|
|
||||||
You can also filter using the `WHERE` clause. For example, selecting only the customers who are from "Germany": |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT * |
|
||||||
FROM Customers |
|
||||||
WHERE Country='Germany'; |
|
||||||
``` |
|
||||||
|
|
||||||
Finally, you can also sort the results using the `ORDER BY` keyword: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT * |
|
||||||
FROM Customers |
|
||||||
ORDER BY Country; |
|
||||||
``` |
|
||||||
|
|
||||||
This will sort the output in ascending order by the Country column. To sort in descending order, you would add the `DESC` keyword: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT * |
|
||||||
FROM Customers |
|
||||||
ORDER BY Country DESC; |
|
||||||
``` |
|
||||||
|
|
||||||
These are the very basics of the `SELECT` statement in SQL, which is a vital part of working with databases. |
|
@ -1,37 +0,0 @@ |
|||||||
# INSERT |
|
||||||
|
|
||||||
The `INSERT` statement in SQL is used to add new rows of data to a table in the database. There are three forms of the `INSERT` statement: `INSERT INTO` values, `INSERT INTO` set, and `INSERT INTO` select. |
|
||||||
|
|
||||||
## `INSERT INTO` values |
|
||||||
|
|
||||||
The basic syntax for `INSERT INTO` values: |
|
||||||
|
|
||||||
```sql |
|
||||||
INSERT INTO table_name (column1, column2, column3, ...) |
|
||||||
VALUES (value1, value2, value3, ...); |
|
||||||
``` |
|
||||||
This form of the `INSERT` statement specifies both the column names and the values to be inserted. |
|
||||||
|
|
||||||
## `INSERT INTO` set |
|
||||||
|
|
||||||
In this form, you're able to insert data using the `SET` keyword. Here, you specify each column you want to insert data into, and then the data for that column. |
|
||||||
|
|
||||||
```sql |
|
||||||
INSERT INTO table_name |
|
||||||
SET column1 = value1, column2 = value2, ...; |
|
||||||
``` |
|
||||||
|
|
||||||
## `INSERT INTO` select |
|
||||||
|
|
||||||
The `INSERT INTO SELECT` statement is used to copy data from one table and insert it into another table. Or, to insert data into specific columns from another table. |
|
||||||
|
|
||||||
```sql |
|
||||||
INSERT INTO table_name1 (column1, column2, column3, ...) |
|
||||||
SELECT column1, column2, column3, ... |
|
||||||
FROM table_name2 |
|
||||||
WHERE condition; |
|
||||||
``` |
|
||||||
|
|
||||||
In all cases, if you're inserting data into a table where some columns have default values, you don't need to specify those columns in your `INSERT INTO` statement. |
|
||||||
|
|
||||||
Note: Be careful when inserting data into a database as SQL does not have a confirm command. So once you execute the insert statement, the records are inserted, and you can't undo the operation. |
|
@ -1,45 +0,0 @@ |
|||||||
# UPDATE |
|
||||||
|
|
||||||
The SQL `UPDATE` statement is used to modify the existing data in a database. This statement is very useful when you need to change the values assigned to specific fields in an existing row or set of rows. |
|
||||||
|
|
||||||
The general syntax for the UPDATE statement is as follows: |
|
||||||
|
|
||||||
```sql |
|
||||||
UPDATE table_name |
|
||||||
SET column1 = value1, column2 = value2, ... |
|
||||||
WHERE condition; |
|
||||||
``` |
|
||||||
|
|
||||||
- `table_name`: The name of the table where an update will be performed. |
|
||||||
- `SET`: This clause specifies the column name and the new value that it should be updated to. |
|
||||||
- `column1, column2, ...`: The column names in the table. |
|
||||||
- `value1, value2, ...`: The new values that you want to record into the database. |
|
||||||
- `WHERE`: This clause specifies the conditions that identify which row(s) to update. |
|
||||||
|
|
||||||
## Example Usage |
|
||||||
|
|
||||||
Here's an example that might provide some clarity. For an imaginary `Employees` table: |
|
||||||
|
|
||||||
| EmployeeID | Name | Position | Salary | |
|
||||||
|------------|---------|----------|--------| |
|
||||||
| 1 | Jane | Manager | 50000 | |
|
||||||
| 2 | John | Clerk | 30000 | |
|
||||||
| 3 | Bob | Engineer | 40000 | |
|
||||||
|
|
||||||
If you want to increase Bob's salary by $5000, you would use: |
|
||||||
|
|
||||||
```sql |
|
||||||
UPDATE Employees |
|
||||||
SET Salary = 45000 |
|
||||||
WHERE EmployeeID = 3; |
|
||||||
``` |
|
||||||
|
|
||||||
This would permanently change the data in the `Employees` table: |
|
||||||
|
|
||||||
| EmployeeID | Name | Position | Salary | |
|
||||||
|------------|---------|----------|--------| |
|
||||||
| 1 | Jane | Manager | 50000 | |
|
||||||
| 2 | John | Clerk | 30000 | |
|
||||||
| 3 | Bob | Engineer | 45000 | |
|
||||||
|
|
||||||
Always be careful with the `UPDATE` statement; if you forget the `WHERE` clause, you will update all the rows in the table. |
|
@ -1,30 +0,0 @@ |
|||||||
# DELETE |
|
||||||
|
|
||||||
The `DELETE` statement in SQL helps you remove existing records from a database. However, keep in mind, it is a destructive operation and may permanently erase data from your database. |
|
||||||
|
|
||||||
With the `DELETE` statement, you can perform the following: |
|
||||||
|
|
||||||
1. **Delete All Rows:** |
|
||||||
|
|
||||||
The `DELETE` statement without a `WHERE` clause deletes all rows in a table. This operation is irreversible. |
|
||||||
|
|
||||||
Example: |
|
||||||
```sql |
|
||||||
DELETE FROM table_name; |
|
||||||
``` |
|
||||||
This SQL statement deletes all the records from `table_name`. |
|
||||||
|
|
||||||
2. **Delete Specific Rows:** |
|
||||||
|
|
||||||
When combined with the `WHERE` clause, the `DELETE` SQL statement erases specific rows that meet the condition. |
|
||||||
|
|
||||||
Example: |
|
||||||
```sql |
|
||||||
DELETE FROM table_name WHERE condition; |
|
||||||
``` |
|
||||||
This instance of the `DELETE` statement deletes records from `table_name` the place where the given `condition` matches. |
|
||||||
|
|
||||||
It's crucial to use `DELETE` cautiously because it has the potential to either erase certain important rows or entirely empty the table. |
|
||||||
|
|
||||||
|
|
||||||
*Note: The deletion made by the "DELETE" statement is permanent and cannot be undone. Always ensure to have a backup before running a DELETE query, especially when it is on a production database.* |
|
@ -1 +0,0 @@ |
|||||||
# Statements |
|
@ -1,73 +0,0 @@ |
|||||||
# SELECT |
|
||||||
|
|
||||||
`SELECT` is one of the most widely used commands in SQL. This command is used to select data from a database. The data returned is stored in a results table, also called the result-set. |
|
||||||
|
|
||||||
## Syntax |
|
||||||
|
|
||||||
The simplest way to use the `SELECT` statement is to return all columns from a table. This can be done with the following syntax: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT * FROM table_name; |
|
||||||
``` |
|
||||||
|
|
||||||
This will return all fields (columns) of all records (rows) from the table. |
|
||||||
|
|
||||||
If you want to select just certain columns, you can specify them by name, separated by commas: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT column_name1, column_name2 FROM table_name; |
|
||||||
``` |
|
||||||
|
|
||||||
## SELECT DISTINCT |
|
||||||
|
|
||||||
The `SELECT DISTINCT` statement is used to return only unique values in the output. It can be used to eliminate duplicate values in the returned data. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT DISTINCT column_name FROM table_name; |
|
||||||
``` |
|
||||||
|
|
||||||
## WHERE Clause |
|
||||||
|
|
||||||
The `WHERE` clause is used to filter records. The `WHERE` clause is used to extract only those records that fulfill a specified condition. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT column_name FROM table_name WHERE condition; |
|
||||||
``` |
|
||||||
|
|
||||||
## ORDER BY |
|
||||||
|
|
||||||
The `ORDER BY` keyword is used to sort the result-set in ascending or descending order. The `ORDER BY` keyword sorts the records in ascending order by default. If you want to sort the records in descending order, you can use the `DESC` keyword. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT column_name FROM table_name ORDER BY column_name ASC|DESC; |
|
||||||
``` |
|
||||||
|
|
||||||
## Aggregate Functions |
|
||||||
|
|
||||||
Aggregate functions in SQL are functions where the values of multiple rows are grouped together to form a single value of more significant meaning, such as a list, a set, or a sum. Some examples include `SUM()`, `COUNT()`, `MIN()`, `MAX()`, and `AVG()`. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT COUNT(column_name) FROM table_name WHERE condition; |
|
||||||
SELECT AVG(column_name) FROM table_name WHERE condition; |
|
||||||
SELECT SUM(column_name) FROM table_name WHERE condition; |
|
||||||
SELECT MIN(column_name) FROM table_name WHERE condition; |
|
||||||
SELECT MAX(column_name) FROM table_name WHERE condition; |
|
||||||
``` |
|
||||||
|
|
||||||
## GROUP BY |
|
||||||
|
|
||||||
The `GROUP BY` statement is often used with aggregate functions (`COUNT`, `MAX`, `MIN`, `SUM`, `AVG`) to group the result-set by one or more columns. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT column_name1, COUNT(column_name2) FROM table_name WHERE condition GROUP BY column_name1 ORDER BY COUNT(column_name2) DESC; |
|
||||||
``` |
|
||||||
|
|
||||||
## HAVING Clause |
|
||||||
|
|
||||||
The `HAVING` clause was added to SQL because the `WHERE` keyword could not be used with aggregate functions. It works like the WHERE clause but on grouped records. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT column_name, COUNT(column_name) FROM table_name GROUP BY column_name HAVING COUNT(column_name) > value; |
|
||||||
``` |
|
||||||
|
|
||||||
The above are the most common uses of the `SELECT` keyword in SQL. There are other keywords and functions you can use to manipulate the data as well. These will give you a good start on using `SELECT` in your SQL queries. |
|
@ -1,62 +0,0 @@ |
|||||||
# Types of Sub Queries |
|
||||||
|
|
||||||
Subqueries, sometimes referred to as inner queries or nested queries, are queries that are embedded within the clause of another SQL query. There are different types of SQL subqueries that are frequently used including Scalar, Row, Column, and Table subqueries. |
|
||||||
|
|
||||||
## Scalar Subqueries |
|
||||||
|
|
||||||
A scalar subquery is a query that returns exactly one column with a single value. This type of subquery can be used anywhere in your SQL where expressions are allowed. |
|
||||||
|
|
||||||
Example: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT column_name [, column_name ] |
|
||||||
FROM table1 [, table2 ] |
|
||||||
WHERE column_name operator |
|
||||||
(SELECT column_name [, column_name ] |
|
||||||
FROM table_name |
|
||||||
WHERE condition); |
|
||||||
``` |
|
||||||
|
|
||||||
## Row Subqueries |
|
||||||
|
|
||||||
Row subqueries are used to return one or more rows to the outer SQL select query. However, the subquery returns multiple columns and rows, so it cannot be directly used where scalar expressions are used. |
|
||||||
|
|
||||||
Example: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT column_name [, column_name ] |
|
||||||
FROM table1 [, table2 ] |
|
||||||
WHERE (column_name [, column_name ]) |
|
||||||
IN (SELECT column_name [, column_name ] |
|
||||||
FROM table_name |
|
||||||
WHERE condition); |
|
||||||
``` |
|
||||||
|
|
||||||
## Column Subqueries |
|
||||||
|
|
||||||
Column Subqueries are used to return one or more columns to the outer SQL select query. They are used when the subquery is expected to return more than one column to the main query. |
|
||||||
|
|
||||||
Example: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT column_name [, column_name ] |
|
||||||
FROM table1 [, table2 ] |
|
||||||
WHERE (SELECT column_name [, column_name ] |
|
||||||
FROM table_name |
|
||||||
WHERE condition); |
|
||||||
``` |
|
||||||
|
|
||||||
## Table Subqueries |
|
||||||
|
|
||||||
Table subqueries are used in the FROM clause and return a table that can be used as a table-reference in an SQL statement. They come in handy when you want to perform operations such as joining multiple tables, union data from multiple sources, etc. |
|
||||||
|
|
||||||
Example: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT column_name [, column_name ] |
|
||||||
FROM |
|
||||||
(SELECT column_name [, column_name ] |
|
||||||
FROM table1 [, table2 ]) |
|
||||||
WHERE condition; |
|
||||||
``` |
|
||||||
Remember that not all SQL databases support all types of subqueries. Learning how and when to utilize each form is an essential aspect of constructing effective SQL queries. |
|
@ -1,71 +0,0 @@ |
|||||||
# Numeric |
|
||||||
|
|
||||||
SQL numeric functions are used to perform operations on numeric data types such as integer, decimal, and float. They're fundamental in manipulating data in SQL commands and are commonly used in `SELECT`, `UPDATE`, `DELETE` and `INSERT` statements. |
|
||||||
|
|
||||||
## Examples of SQL Numeric Functions: |
|
||||||
|
|
||||||
1. **ABS() Function:** This function returns the absolute (positive) value of a number. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT ABS(-243); |
|
||||||
``` |
|
||||||
Output: |
|
||||||
|
|
||||||
243 |
|
||||||
|
|
||||||
2. **Avg() Function:** This function returns the average value of a column. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT AVG(price) FROM products; |
|
||||||
``` |
|
||||||
|
|
||||||
3. **COUNT() Function:** This function returns the number of rows that matches a specified criterion. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT COUNT(productID) FROM products; |
|
||||||
``` |
|
||||||
|
|
||||||
4. **SUM() Function:** This function returns the total sum of a numeric column. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT SUM(price) FROM products; |
|
||||||
``` |
|
||||||
|
|
||||||
5. **MIN() & MAX() Functions:** MIN() function returns the smallest value of the selected column, and MAX() function returns the largest value of the selected column. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT MIN(price) FROM products; |
|
||||||
SELECT MAX(price) FROM products; |
|
||||||
``` |
|
||||||
|
|
||||||
6. **ROUND() Function:** This function is used to round a numeric field to the nearest integer, you can, however, specify the number of decimals to be returned. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT ROUND(price, 2) FROM products; |
|
||||||
``` |
|
||||||
|
|
||||||
7. **CEILING() Function:** This function returns the smallest integer which is greater than, or equal to, the specified numeric expression. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT CEILING(price) FROM products; |
|
||||||
``` |
|
||||||
|
|
||||||
8. **FLOOR() Function:** This function returns the largest integer which is less than, or equal to, the specified numeric expression. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT FLOOR(price) FROM products; |
|
||||||
``` |
|
||||||
|
|
||||||
9. **SQRT() Function:** This function returns the square root of a number. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT SQRT(price) FROM products; |
|
||||||
``` |
|
||||||
|
|
||||||
10. **PI() Function:** This function returns the constant Pi. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT PI(); |
|
||||||
``` |
|
||||||
|
|
||||||
These are just a few examples, SQL supports many more mathematical functions such as SIN, COS, TAN, COT, POWER, etc. Understanding and using these SQL numeric functions allows you to perform complex operations on the numeric data in your SQL tables. |
|
@ -1,91 +0,0 @@ |
|||||||
# String Functions |
|
||||||
|
|
||||||
In SQL, you can perform various operations on strings, including extracting a string, combining two or more strings, and converting a case of a string. |
|
||||||
|
|
||||||
## CONCAT Function |
|
||||||
|
|
||||||
The CONCAT function combines two or more strings into one string. The following is the syntax: |
|
||||||
|
|
||||||
```sql |
|
||||||
CONCAT(string1, string2, ...., string_n) |
|
||||||
``` |
|
||||||
|
|
||||||
Example: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT CONCAT('Hello ', 'World'); |
|
||||||
``` |
|
||||||
|
|
||||||
The output of the above SQL statement will be 'Hello World'. |
|
||||||
|
|
||||||
## SUBSTRING Function |
|
||||||
|
|
||||||
The SUBSTRING function extracts a string from a given string. The syntax looks as follows: |
|
||||||
|
|
||||||
```sql |
|
||||||
SUBSTRING(string, start, length) |
|
||||||
``` |
|
||||||
|
|
||||||
Example: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT SUBSTRING('SQL Tutorial', 1, 3); |
|
||||||
``` |
|
||||||
|
|
||||||
The output of the above query will be 'SQL'. |
|
||||||
|
|
||||||
## LENGTH Function |
|
||||||
|
|
||||||
The LENGTH function returns the length of a string. The syntax is: |
|
||||||
|
|
||||||
```sql |
|
||||||
LENGTH(string) |
|
||||||
``` |
|
||||||
|
|
||||||
Example: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT LENGTH('Hello World'); |
|
||||||
``` |
|
||||||
|
|
||||||
The output of the above SQL statement will be 11. |
|
||||||
|
|
||||||
## UPPER and LOWER Function |
|
||||||
|
|
||||||
The UPPER function converts all the letters in a string to uppercase, whereas the LOWER function to lowercase. |
|
||||||
|
|
||||||
Syntax: |
|
||||||
|
|
||||||
```sql |
|
||||||
UPPER(string) |
|
||||||
|
|
||||||
LOWER(string) |
|
||||||
``` |
|
||||||
|
|
||||||
Examples: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT UPPER('Hello World'); |
|
||||||
|
|
||||||
SELECT LOWER('Hello World'); |
|
||||||
``` |
|
||||||
The output of the above SQL statements will be 'HELLO WORLD' and 'hello world' respectively. |
|
||||||
|
|
||||||
## TRIM Function |
|
||||||
|
|
||||||
The TRIM function removes leading and trailing spaces of a string. You can also remove other specified characters. |
|
||||||
|
|
||||||
Syntax: |
|
||||||
|
|
||||||
```sql |
|
||||||
TRIM([LEADING|TRAILING|BOTH] [removal_string] FROM original_string) |
|
||||||
``` |
|
||||||
|
|
||||||
Example: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT TRIM(' Hello World '); |
|
||||||
SELECT TRIM('h' FROM 'hello'); |
|
||||||
``` |
|
||||||
|
|
||||||
The output of the first query will be 'Hello World' and that of the second query will be 'ello'. |
|
@ -1,67 +0,0 @@ |
|||||||
# Conditional |
|
||||||
|
|
||||||
In SQL, Conditional expressions can be used in the SELECT statement, WHERE clause, and ORDER BY clause to evaluate multiple conditions. These are SQL's version of the common if…then…else statement in other programming languages. |
|
||||||
|
|
||||||
There are two kinds of conditional expressions in SQL: |
|
||||||
|
|
||||||
1. **CASE** expression |
|
||||||
|
|
||||||
The `CASE` expression is a flow-control statement that allows you to add if-else logic to a query. It comes in two forms: simple and searched. |
|
||||||
|
|
||||||
Here is an example of a simple `CASE` expression: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT OrderID, Quantity, |
|
||||||
CASE |
|
||||||
WHEN Quantity > 30 THEN 'Over 30' |
|
||||||
ELSE 'Under 30' |
|
||||||
END AS QuantityText |
|
||||||
FROM OrderDetails; |
|
||||||
``` |
|
||||||
A searched `CASE` statement: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT FirstName, City, |
|
||||||
CASE |
|
||||||
WHEN City = 'Berlin' THEN 'Germany' |
|
||||||
WHEN City = 'Madrid' THEN 'Spain' |
|
||||||
ELSE 'Unknown' |
|
||||||
END AS Country |
|
||||||
FROM Customers; |
|
||||||
``` |
|
||||||
|
|
||||||
2. **COALESCE** expression |
|
||||||
|
|
||||||
The `COALESCE` function returns the first non-null value in a list. It takes a comma-separated list of values and returns the first value that is not null. |
|
||||||
|
|
||||||
An example of a `COALESCE` statement: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT ProductName, |
|
||||||
COALESCE(UnitsOnOrder, 0) As UnitsOnOrder, |
|
||||||
COALESCE(UnitsInStock, 0) As UnitsInStock, |
|
||||||
FROM Products; |
|
||||||
``` |
|
||||||
|
|
||||||
3. **NULLIF** expression |
|
||||||
|
|
||||||
`NULLIF` returns null if the two given expressions are equal. |
|
||||||
|
|
||||||
Example of using `NULLIF`: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT NULLIF(5,5) AS Same, |
|
||||||
NULLIF(5,7) AS Different; |
|
||||||
``` |
|
||||||
|
|
||||||
4. **IIF** expression |
|
||||||
|
|
||||||
`IIF` function returns value_true if the condition is TRUE, or value_false if the condition is FALSE. |
|
||||||
|
|
||||||
Example of using `IIF`: |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT IIF (1>0, 'One is greater than zero', 'One is not greater than zero'); |
|
||||||
``` |
|
||||||
|
|
||||||
These are essential constructs that can greatly increase the flexibility and functionality of your SQL code, particularly when dealing with elaborate conditions and specific data selections. |
|
@ -1,47 +0,0 @@ |
|||||||
# Date and Time |
|
||||||
|
|
||||||
In SQL, the `DateTime` data type is used to work with dates and times. SQL Server comes with numerous functions for processing dates and times. Some of these include `GETDATE()`, `DATEDIFF()`, `DATEADD()`, `CONVERT()`, and so forth. |
|
||||||
|
|
||||||
## GETDATE() |
|
||||||
|
|
||||||
`GETDATE()` returns the current date and time as a DateTime datatype. It does not require any arguments. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT GETDATE() AS CurrentDateTime; |
|
||||||
``` |
|
||||||
|
|
||||||
## DATEDIFF() |
|
||||||
|
|
||||||
`DATEDIFF()` returns the difference between two date values based on the unit of time you want to use. The syntax is `DATEDIFF(datepart, startdate, enddate)`. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT DATEDIFF(day, '2022-01-01', '2022-01-15') AS DiffInDays; |
|
||||||
``` |
|
||||||
|
|
||||||
## DATEADD() |
|
||||||
|
|
||||||
`DATEADD()` adds or subtracts a specified time interval from a date. Its syntax is `DATEADD(datepart, number, date)`. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT DATEADD(year, 1, '2022-01-01') AS NewDate; |
|
||||||
``` |
|
||||||
|
|
||||||
## CONVERT() |
|
||||||
|
|
||||||
`CONVERT()` is used to convert from one data type to another, and it is commonly used to format DateTime values. Its syntax is `CONVERT(data_type(length), expression, style)`. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT CONVERT(VARCHAR(19), GETDATE()) AS FormattedDateTime; |
|
||||||
``` |
|
||||||
Remember to replace `date` with your date in above queries. |
|
||||||
|
|
||||||
## DateTime Format |
|
||||||
|
|
||||||
By using appropriate format codes, SQL allows us to present dates and times in various formats. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT FORMAT(GETDATE(), 'MM/dd/yyyy') AS DateFormatted; |
|
||||||
``` |
|
||||||
Also, by using specific column names instead of `GETDATE()`, the same patterns can be applied to DateTime values in your data. |
|
||||||
|
|
||||||
Note: All dates are stored as numeric values under the hood, with the integer portion representing the date and the decimal portion representing the time. Also, different database systems may use slightly different functions for handling dates and times, so be sure to check the documentation for your specific DBMS. |
|
@ -1,65 +0,0 @@ |
|||||||
# Query Optimization Techniques |
|
||||||
|
|
||||||
SQL Query Optimization is a crucial aspect of database management, aiming to maximize the speed and efficiency of SQL queries. Optimization takes into consideration a variety of factors like the query structure, data distribution, hardware specifics, and data-indexing. |
|
||||||
|
|
||||||
## 1. Utilize Indexes: |
|
||||||
|
|
||||||
Indexes enable the database engine to quickly find records just like an index in a book helps facilitate faster information location. |
|
||||||
|
|
||||||
```sql |
|
||||||
CREATE INDEX index_name |
|
||||||
ON table_name (column1, column2, ...); |
|
||||||
``` |
|
||||||
|
|
||||||
## 2. Use Joins instead of Multiple Queries: |
|
||||||
|
|
||||||
Multiple separate queries to retrieve data can be merged into a single JOIN query for a more efficient process. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT Orders.OrderID, Customers.CustomerName |
|
||||||
FROM Orders |
|
||||||
INNER JOIN Customers |
|
||||||
ON Orders.CustomerID=Customers.CustomerID; |
|
||||||
``` |
|
||||||
|
|
||||||
## 3. Limit the Number of Rows: |
|
||||||
|
|
||||||
Only request the number of rows you need. The `LIMIT` clause reduces the number of records returned by a SQL statement. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT column FROM table |
|
||||||
ORDER BY column DESC |
|
||||||
LIMIT 10; |
|
||||||
``` |
|
||||||
|
|
||||||
## 4. Avoid SELECT * : |
|
||||||
|
|
||||||
Select only the columns you need to prevent the overhead of loading unnecessary data. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT column1, column2 |
|
||||||
FROM table; |
|
||||||
``` |
|
||||||
|
|
||||||
## 5. Use WHERE instead of HAVING for Filtering: |
|
||||||
`WHERE` clause filters records before grouping while `HAVING` filters after. Utilizing `WHERE` can optimize the query performance. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT column1, COUNT(column2) |
|
||||||
FROM table |
|
||||||
WHERE condition |
|
||||||
GROUP BY column1; |
|
||||||
``` |
|
||||||
|
|
||||||
## 6. If Possible Avoid the use of Subqueries: |
|
||||||
|
|
||||||
A subquery is a SQL query enclosed in a larger SQL query. Often they may result in complex and less optimized queries. |
|
||||||
|
|
||||||
```sql |
|
||||||
SELECT column_name(s) |
|
||||||
FROM table1 |
|
||||||
WHERE column_name operator |
|
||||||
(SELECT column_name(s) from table2); |
|
||||||
``` |
|
||||||
|
|
||||||
Remember there's no universal optimal solution, each SQL query will have its specific optimization strategies. |
|
@ -1 +0,0 @@ |
|||||||
# |
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue