SQL Commands
SQL Commands
Data Warehouse
• A data warehouse is a large, centralized repository of data that is used
for business intelligence and decision-making purposes. The purpose
1
01-04-2023
ERD ERD
• An ERD (Entity-Relationship Diagram) is a graphical representation of An ERD typically consists of the following components:
entities, attributes, and relationships that exist in a database. ERDs • Entities: Entities are objects or concepts that have attributes and
are used in database design to model and define the relationships relationships with other entities. In an ERD, entities are represented
between different entities and their attributes. by rectangles.
• Attributes: Attributes are characteristics or properties of an entity. In
an ERD, attributes are represented by ovals or ellipses, and are
connected to their respective entities.
• Relationships: Relationships are the associations between two or
more entities. In an ERD, relationships are represented by lines that
connect two or more entities.
ERD ERD
There are three types of relationships that can exist between entities: • In an ERD, relationships can also have attributes, which describe the
• One-to-one: This relationship exists when one entity is associated characteristics of the relationship itself.
with only one instance of another entity. • ERDs are an important tool in database design, as they allow
• One-to-many: This relationship exists when one entity is associated designers to visualize the relationships between entities and their
with multiple instances of another entity. attributes. This helps ensure that the database design is accurate and
efficient, and that the database can be easily queried and analyzed.
• Many-to-many: This relationship exists when multiple instances of
one entity are associated with multiple instances of another entity.
2
01-04-2023
3
01-04-2023
4
01-04-2023
5
01-04-2023
Semantic constraints
• Semantic constraints are an important tool for ensuring the accuracy
and consistency of data in a database. They help to prevent errors,
improve data quality, and ensure that the data is meaningful and
relevant to the domain being represented.
SQL queries
6
01-04-2023
7
01-04-2023
8
01-04-2023
9
01-04-2023
SQL for Retrieving Data from One Table Conceptual Evaluation Strategy
SELECT column_name, column_name, … • Semantics of an SQL query defined in terms of the
FROM table_name
following conceptual evaluation strategy:
• Compute the cross-product of relation-list.
WHERE condition/criteria;
• Discard resulting tuples if they fail qualifications.
• Delete attributes that are not in target-list.
• This statement will retrieve the specified field values for all rows in the specified • If DISTINCT is specified, eliminate duplicate rows.
table that meet the specified conditions.
• This strategy is probably the least efficient way to
• Every SELECT statement returns a recordset. compute a query! An optimizer will find more efficient
strategies to compute the same answers.
10
01-04-2023
11
01-04-2023
12
01-04-2023
13
01-04-2023
1 2
• SELECT * FROM table_name; • SELECT column1, column2, column3 FROM table_name;
14
01-04-2023
3 4
• SELECT * FROM table_name WHERE column1 = 'value'; • SELECT * FROM table_name ORDER BY column1 DESC;
15
01-04-2023
7 8
• SELECT * FROM table_name WHERE column1 IN ('value1', 'value2', • SELECT * FROM table_name WHERE column1 BETWEEN value1 AND
'value3'); value2;
• This will select all rows from the specified table where column1 is • This will select all rows from the specified table where column1 is
equal to any of the specified values. between the specified values.
9 10
• SELECT DISTINCT column1 FROM table_name; • SELECT column1, COUNT(*) FROM table_name GROUP BY column1;
16
01-04-2023
• How do you perform a left or right join in SQL? • How do you perform an inner join in SQL?
• To perform a left or right join in SQL, you can use the LEFT JOIN or • To perform an inner join in SQL, you can use the INNER JOIN
RIGHT JOIN keywords instead of JOIN. For example: keyword. For example:
SELECT * SELECT *
FROM table1 FROM table1
LEFT JOIN table2 INNER JOIN table2
ON table1.id = table2.id; ON table1.id = table2.id;
17
01-04-2023
• How do you use the HAVING clause in SQL? • How do you use the SUM() function to calculate a total in SQL?
• The HAVING clause is used to filter the results of a GROUP BY clause • The SUM() function is used to calculate the sum of a column. For
based on a condition. For example: example:
• How do you use the MAX() and MIN() functions to find the maximum • How do you use the COUNT() function to count the number of rows in
and minimum values in SQL? a table in SQL?
• The MAX() and MIN() functions are used to find the maximum and • The COUNT() function is used to count the number of rows in a table.
minimum values of a column. For example: For example:
SELECT COUNT(*)
SELECT MAX(column1), MIN(column1) FROM table1;
FROM table1;
18
01-04-2023
• How do you use subqueries in SQL? 1. Write a query to select all columns from a table named "customers".
2. Write a query to select the columns "name" and "age" from a table named
• Subqueries are used to retrieve data from a table based on the results "students".
of another query. For example: 3. Write a query to select all rows from a table named "orders" where the column
"status" is equal to "shipped".
SELECT column1 4. Write a query to select all rows from a table named "employees" and order
FROM table1 them by the column "hire_date" in descending order.
5. Write a query to select the top 10 rows from a table named "products" and
WHERE column1 IN (SELECT column1 FROM table2 WHERE column2 = order them by the column "price" in ascending order.
'value'); 6. Write a query to select all rows from a table named "customers" where the
column "email" contains the string "gmail.com".
7. Write a query to select all rows from a table named "orders" where the column
"status" is either "shipped" or "delivered".
8. Write a query to select all rows from a table named "employees" 11. Write a query to retrieve the names of all the employees from a table
named "employees" whose names start with the letter 'J'.
where the column "salary" is between 50000 and 100000.
12. Write a query to retrieve the names and prices of all the products from a
9. Write a query to select all distinct values from a column named table named "products" whose prices are between 50 and 100.
"category" in a table named "products". 13. Write a query to retrieve the number of orders for each customer from a
table named "orders" grouped by the "customer_id" column.
10. Write a query to select the total sales for each product in a table 14. Write a query to retrieve the names and total sales of all the products
named "sales", grouping by the column "product_id". from a table named "sales" grouped by the "product_id" column.
15. Write a query to retrieve the names and birth dates of all the customers
from a table named "customers" whose birth dates are in the year 1990.
16. Write a query to retrieve the names and hire dates of all the employees
from a table named "employees" who were hired in the year 2018.
19
01-04-2023
2. How do you use the UNION operator to combine two or more SELECT 3. How do you use the JOIN ON clause to specify a condition for a join
statements in SQL? in SQL?
The UNION operator is used to combine the results of two or more SELECT
statements into a single result set. The columns in each SELECT statement
must match in number and data type. For example, to combine the results of • The JOIN ON clause is used to specify a condition for joining two
two SELECT statements: tables together. It is used in conjunction with the JOIN keyword. For
example, to join two tables on a common column:
SELECT column1, column2 SELECT *
FROM table1
FROM table1
UNION
SELECT column1, column2 JOIN table2
FROM table2; ON table1.column1 = table2.column1;
20
01-04-2023
4. How do you use the CASE statement in SQL? 5 How do you use the GROUP_CONCAT() function to concatenate rows
The CASE statement is used to conditionally return a value based on one or more into a single string in SQL?
conditions. It can be used in the SELECT, WHERE, and ORDER BY clauses. For
example, to return a different value based on the value of a column: The GROUP_CONCAT() function is used to concatenate multiple rows
SELECT column1, into a single string. It is often used with the GROUP BY clause. For
CASE example, to concatenate the values of a column for each group:
WHEN column2 = 'value1' THEN 'Result1' SELECT column1, GROUP_CONCAT(column2)
WHEN column2 = 'value2' THEN 'Result2' FROM table1
ELSE 'Other'
GROUP BY column1;
END AS Result
FROM table1;
6. How do you use the COALESCE() function in SQL? 7. How do you use the DATEADD() function to add or subtract time
• The COALESCE() function is used to return the first non-null value in a from a date in SQL?
list of values. It can be used to handle null values in a query. For The DATEADD() function is used to add or subtract a specified interval
example, to return a default value if a column is null: of time from a date. It takes three arguments: the interval to
SELECT COALESCE(column1, 'default') add/subtract, the number of intervals, and the date to modify. For
example, to add 7 days to a date:
FROM table1;
21
01-04-2023
8. How do you use the CAST() function to convert data types in SQL? 9. How do you use the RANK() function to rank rows in a result set in
The CAST() function is used to convert one data type to another. It SQL?
takes two arguments: the value to convert, and the data type to • The RANK() function is used to assign a rank to each row in a result
convert to. For example, to convert a string to an integer: set based on a specified criteria. The rank of a row is determined by
its position in the result set when ordered by the criteria specified in
the function.
• To use the RANK() function in SQL, you need to include it in your
SELECT CAST('123' AS int); SELECT statement and specify an ORDER BY clause to determine the
order in which the rows should be ranked. Here's an example:
SELECT RANK() OVER (ORDER BY column_name DESC) AS rank_number, * 10. How do you use the ROW_NUMBER() function to assign a unique
FROM table_name; number to each row in a result set in SQL?
• In this example, the RANK() function is used to assign a rank to each row in
the result set, and the ORDER BY clause specifies the column that the rows The ROW_NUMBER() function is used to assign a unique number to
should be ordered by. The "DESC" keyword is used to order the rows in each row in a result set in SQL. This function is very useful for ranking,
descending order. pagination, and other operations that require a unique identifier for
• The result set will include a new column called "rank_number" that each row.
contains the rank assigned to each row. Rows with the same value for the
ranking criteria will receive the same rank, and the next rank will be To use the ROW_NUMBER() function, you need to include it in your
skipped. For example, if two rows have the same value for the ranking SELECT statement and specify an ORDER BY clause to determine the
criteria and are ranked 1 and 2, the next rank assigned will be 3. order in which the rows should be numbered. Here's an example:
• Note that the RANK() function is available in most SQL databases, but the SELECT ROW_NUMBER() OVER (ORDER BY column_name) AS row_number, *
syntax may vary slightly depending on the database you are using.
FROM table_name;
22
01-04-2023
Constraints
The following constraints are commonly used in SQL:
•NOT NULL - Ensures that a column cannot have a NULL value
•UNIQUE - Ensures that all values in a column are different
•PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely
identifies each row in a table
•FOREIGN KEY - Prevents actions that would destroy links between
tables
•CHECK - Ensures that the values in a column satisfies a specific
condition
•DEFAULT - Sets a default value for a column if no value is
specified
•CREATE INDEX - Used to create and retrieve data from the
database very quickly
23