A Beginner's Handbook to SQL Operators

Start from the beginning
                                        

'''sql

SELECT FROM employees WHERE age > 30 AND department = 'HR';

'''

- OR: Combines two conditions; either can be true.

'''sql

SELECT FROM employees WHERE age > 30 OR department = 'HR';

'''

- NOT: Negates a condition.

'''sql

SELECT FROM employees WHERE NOT department = 'HR';

'''

4. Bitwise Operators

Bitwise operators perform bit-level operations on integer data.

- AND ('&'): Bitwise AND.

'''sql

SELECT 5 & 3; -- Result: 1

'''

- OR ('|'): Bitwise OR.

'''sql

SELECT 5 | 3; -- Result: 7

'''

- XOR ('^'): Bitwise XOR.

'''sql

SELECT 5 ^ 3; -- Result: 6

'''

- NOT ('~'): Bitwise NOT.

'''sql

SELECT ~5; -- Result: -6 (depends on the SQL implementation)

'''

5. Set Operators

Set operators combine results from two or more queries.

- UNION: Combines results of two queries and removes duplicates.

'''sql

SELECT name FROM employees

UNION

SELECT name FROM managers;

'''

- UNION ALL: Combines results of two queries, including duplicates.

'''sql

SELECT name FROM employees

UNION ALL

SELECT name FROM managers;

'''

- INTERSECT: Returns common records from two queries.

'''sql

SELECT name FROM employees

INTERSECT

SELECT name FROM managers;

'''

- EXCEPT (or MINUS): Returns records from the first query that are not in the second query.

'''sql

SELECT name FROM employees

EXCEPT

SELECT name FROM managers;

'''

6. Other Useful Operators

- LIKE: Searches for a specified pattern in a column.

'''sql

SELECT FROM employees WHERE name LIKE 'A%'; -- Names starting with 'A'

'''

- IN: Checks if a value matches any value in a list.

'''sql

SELECT FROM employees WHERE department IN ('HR', 'Finance');

'''

- BETWEEN: Selects values within a range.

'''sql

SELECT FROM employees WHERE age BETWEEN 30 AND 40;

'''

- IS NULL: Checks for null values.

'''sql

SELECT FROM employees WHERE manager_id IS NULL;

'''

- IS NOT NULL: Checks for non-null values.

'''sql

SELECT FROM employees WHERE manager_id IS NOT NULL;

'''

Conclusion

This beginner's handbook covers essential SQL operators that form the foundation of querying and manipulating data within a relational database. Understanding and using these operators effectively will enable you to perform a wide range of database operations, from simple queries to complex data manipulations. Happy querying!

You've reached the end of published parts.

⏰ Last updated: Jul 05, 2024 ⏰

Add this story to your Library to get notified about new parts!

A Beginner's Handbook to SQL OperatorsWhere stories live. Discover now