Misc in SQL
Sure! Here’s a quick overview of Miscellaneous (Misc) Concepts and Commands in SQL that don’t always fit neatly into one category but are useful to know:
? Miscellaneous SQL Concepts & Commands
1. Comments
Single-line comment:
-- This is a single-line commentMulti-line comment:
/* This is a multi-line comment */
2. Aliases
Temporary names for columns or tables for readability:
SELECT first_name AS fname, last_name AS lname FROM employees;
3. Transactions
Control groups of SQL statements to be executed as a unit:
BEGIN TRANSACTION;UPDATE accounts SET balance = balance - 100 WHERE id = 1;UPDATE accounts SET balance = balance + 100 WHERE id = 2;COMMIT;ROLLBACKto undo changes.
4. CASE Statement
Conditional logic inside queries:
SELECT employee_id, CASE WHEN salary > 50000 THEN 'High' ELSE 'Low' END AS salary_levelFROM employees;
5. SET Operations
Combine result sets:
UNION- combine distinct rowsUNION ALL- combine all rows including duplicatesINTERSECT- rows common to bothMINUS/EXCEPT- rows in first but not in second
6. Casting/Converting Data Types
Convert between types:
SELECT CAST(price AS INT) FROM products;SELECT CONVERT(VARCHAR, order_date, 101) FROM orders; -- SQL Server
7. Indexes (Misc)
Improve performance on large tables.
CREATE INDEX idx_lastname ON employees(last_name);
8. Constraints
Rules on columns:
NOT NULL,UNIQUE,PRIMARY KEY,FOREIGN KEY,CHECK,DEFAULT
9. Temporary Tables
Tables that exist temporarily during a session.
CREATE TEMPORARY TABLE temp_customers AS SELECT * FROM customers WHERE active = 1;
10. EXPLAIN / DESCRIBE
Show query execution plan or table structure.
EXPLAIN SELECT * FROM orders WHERE amount > 1000;DESCRIBE employees;
If you want, I can dive into any of these or share more misc SQL commands and concepts!