SQL ‐ CURD Operation - CloudScope/DevOpsWithCloudScope GitHub Wiki
1. Create (INSERT)
- Purpose: To add new records to a table.
- Syntax:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
- Example:
INSERT INTO employees (first_name, last_name, email) VALUES ('John', 'Doe', '[email protected]');
2. Read (SELECT)
- Purpose: To retrieve data from one or more tables.
- Syntax:
SELECT column1, column2, ... FROM table_name WHERE condition;
- Example:
SELECT first_name, last_name FROM employees WHERE department = 'Sales';
3. Update (UPDATE)
- Purpose: To modify existing records in a table.
- Syntax:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
- Example:
UPDATE employees SET email = '[email protected]' WHERE employee_id = 1;
4. Delete (DELETE)
- Purpose: To remove records from a table.
- Syntax:
DELETE FROM table_name WHERE condition;
- Example:
DELETE FROM employees WHERE employee_id = 1;
5. DISTINCT
- Purpose: The
DISTINCT
keyword is used to remove duplicate rows from the result set. It ensures that the results returned by a query contain only unique values. - Syntax:
SELECT DISTINCT column1, column2, ... FROM table_name;
- Example:
This query retrieves unique cities from theSELECT DISTINCT city FROM customers;
customers
table.
6. PRINT
- Purpose: The
PRINT
statement is primarily used in SQL Server to display messages to the user. It's not part of standard SQL and is specific to certain database systems. - Syntax:
PRINT 'Your message here';
- Example:
This will output the message "Query executed successfully!" to the console.PRINT 'Query executed successfully!';
7. IN
- Purpose: The
IN
operator allows you to specify multiple values in aWHERE
clause. It is used to filter records based on a list of specified values. - Syntax:
SELECT column1, column2, ... FROM table_name WHERE column_name IN (value1, value2, ...);
- Example:
This query retrieves all products that belong to either the "Electronics" or "Apparel" categories.SELECT * FROM products WHERE category IN ('Electronics', 'Apparel');
8. WHERE
- Purpose: The
WHERE
clause is used to filter records based on specified conditions. It restricts the rows returned by aSELECT
,UPDATE
, orDELETE
statement. - Syntax:
SELECT column1, column2, ... FROM table_name WHERE condition;
- Example:
This query retrieves all employees with a salary greater than 50,000.SELECT * FROM employees WHERE salary > 50000;
Combining Keywords
You can combine these keywords to create more complex queries. Here’s an example that combines DISTINCT
, WHERE
, and IN
:
SELECT DISTINCT city
FROM customers
WHERE country IN ('USA', 'Canada');