Day 38: SQL Command Types for PM | SQL Commands | Operators | Sorting | Limiting
Today, we dive into the various SQL command types and their significance. SQL commands are categorized based on the kind of operation they perform, and understanding these types is crucial for efficient database management. The operations performed in SQL are divided into five major command types:
1. Data Definition Language (DDL)
DDL commands define, modify, and delete the structure of database objects like tables, indexes, and constraints. These commands are focused on the structure, not the data itself.
Examples:
2. Data Manipulation Language (DML)
DML commands manage the data stored in schema objects, allowing you to insert, update, or delete records.
Examples:
3. Data Control Language (DCL)
DCL commands control access to the data in the database by managing user rights and permissions.
Examples:
4. Transaction Control Language (TCL)
TCL commands manage transactions in SQL, ensuring the integrity and consistency of data.
Examples:
5. Data Query Language (DQL)
DQL primarily uses one command, SELECT, which retrieves data from databases. It is key for querying and fetching records.
DDL in Detail: The ALTER Command
The ALTER command allows changes to the structure of a table, such as modifying column names, updating data types, adding or removing columns.
Examples:
ALTER TABLE table_name CHANGE COLUMN old_column new_column data_type;
2. Modifying data type or constraints:
ALTER TABLE table_name MODIFY COLUMN existing_column_name data_type constraint;
3. Adding a new column:
ALTER TABLE table_name ADD COLUMN new_column_name data_type constraints;
4. Dropping a column:
ALTER TABLE table_name DROP COLUMN column_name;
DDL - DROP & TRUNCATE
DROP TABLE table_name;
TRUNCATE TABLE table_name;
Arithmetic Operators in SQL
SQL supports basic arithmetic operations like addition, subtraction, multiplication, division, and modulus, which are useful in queries for data analysis.
SELECT (column1 + column2) FROM table_name;
SELECT (column1 - column2) FROM table_name;
SELECT (column1 * column2) FROM table_name;
SELECT (column1 / column2) FROM table_name;
SELECT (column1 % column2) FROM table_name;
Arithmetic Operators With WHERE Clause
SELECT * FROM table_name WHERE column1 + column2 = column_value;
SELECT * FROM table_name WHERE column1 - column2 = column_value;
SELECT FROM table_name WHERE column1 column2 < column_value;
Recommended by LinkedIn
SELECT * FROM table_name WHERE column1 / column2 = column_value;
SELECT * FROM table_name WHERE column1 % 2 = column_value;
Comparison Operators in SQL
Comparison operators allow you to filter data based on conditions.
SELECT * FROM employees WHERE department = 'Sales';
SELECT * FROM employees WHERE salary <> 50000;
SELECT * FROM employees WHERE age > 30;
SELECT * FROM employees WHERE age < 25;
SELECT * FROM employees WHERE salary BETWEEN 30000 AND 60000;
SELECT * FROM employees WHERE department IS NULL;
SELECT * FROM employees WHERE department IS NOT NULL;
Logical Operators in SQL
Logical operators such as AND, OR, and NOT allow you to combine conditions.
SELECT * FROM employees WHERE department = 'Sales' AND salary > 50000;
SELECT * FROM employees WHERE department = 'Sales' OR department = 'Marketing';
SELECT * FROM employees WHERE NOT department = 'Sales';
SELECT * FROM employees WHERE department IN ('Sales', 'Marketing');
Sorting in SQL
The ORDER BY clause helps sort results in ascending or descending order. You can also sort by multiple columns.
SELECT * FROM employees ORDER BY salary ASC;
SELECT * FROM employees ORDER BY salary ASC;
LIMIT and OFFSET in SQL
SELECT * FROM employees LIMIT 10;
SELECT * FROM employees LIMIT 10;
DISTINCT in SQL
The DISTINCT keyword ensures that duplicate rows are eliminated from the result set.
Examples:
SELECT DISTINCT Brand FROM flipkart_mobiles;
2. Unique Directors in Netflix Movies:
SELECT DISTINCT director FROM netflix;
Putting It All Together
Let’s apply these concepts in a practical example:
SELECT Product, (market_price - sale_price) AS Discount_price
FROM bigbasketproducts
WHERE (market_price - sale_price) > 50;
SELECT *, (`Original Price` - `Selling Price`)
FROM flipkart_mobiles
WHERE Brand = "OPPO" AND `Original Price` > 15000
AND (Rating > 4.5 OR (`Original Price` - `Selling Price`) > 5000)
ORDER BY `Selling Price` ASC LIMIT 5;
Conclusion:
Mastering these SQL command types, arithmetic, comparison, and logical operators is essential for anyone dealing with databases. Whether it's structuring the database, managing data, or retrieving specific information, SQL offers a powerful set of tools that, when used correctly, can streamline data operations and enhance performance.