Filtering and Sorting data using SQL statements
Filtering & Sorting Data using SQL
Once you know the basics (SELECT, FROM, WHERE), filtering
and sorting helps you get the exact rows from tables .
- Use WHERE to filter rows.
- Combine with logical operators:
• AND: all
conditions true
• OR: any condition true
• NOT: negate condition
-- All conditions true (AND)
SELECT * FROM employees
WHERE department = 'Sales' AND salary > 50000;
-- Any condition true (OR)
SELECT * FROM employees
WHERE department = 'Sales' OR department = 'Marketing';
-- Negate condition (NOT)
SELECT * FROM employees
WHERE NOT department = 'HR';
- % = any sequence, _ = single char
• Names starting
with 'J'
• Names with 4 letters starting with 'J'
-- Names starting with 'J'
SELECT * FROM customers
WHERE name LIKE 'J%';
-- Names with four letters starting with 'J'
SELECT * FROM customers
WHERE name LIKE 'J___';
- Check if value matches any in list
SELECT * FROM
products WHERE category IN ('Electronics', 'Furniture');
4. Range Checking with BETWEEN
- Filter values in a range (inclusive)
SELECT * FROM orders
WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31';
- Find NULL:
SELECT * FROM
employees WHERE manager_id IS NULL;
- Find NOT NULL:
SELECT * FROM
employees WHERE manager_id IS NOT NULL;
- Ascending (default):
SELECT * FROM
employees ORDER BY salary;
- Descending:
SELECT * FROM
employees ORDER BY salary DESC;
SELECT * FROM
employees ORDER BY department, salary DESC;
List Of Table Operations in SQL
List of Table Operations in SQL [1. CREATE, 2. INSERT,
3.UPDATE, 4. ALTER, 5. DELETE, 6. DROP, 7. TRUNCATE ]
1.
CREATE : To define a new Table.
2.
INSERT : Add rows to the table.
3.
UPDATE : Modify existing rows in the
table.
4.
ALTER : Modify the structure of the
table.
5.
DELETE : Remove Rows from the table.
6.
DROP : Removes the entire table.
7.
TRUNCATE : Removes the all rows of the
table but it keeps the table structure.
1> CREATE : To define a new Table.
|
CREATE
TABLE jobs ( |
|
job_id INT PRIMARY KEY, |
|
job_title VARCHAR(100), |
|
job_description TEXT, |
|
min_salary DECIMAL(10, 2), |
|
max_salary DECIMAL(10, 2) |
|
); |
2 INSERT : Add rows to the table
VALUES
(100, ‘Manager’,
‘Manage the Team', 800000, 1500000);
3> UPDATE : Modify existing rows in the
table.
UPDATE jobs SET min_salary = 100000
WHERE job_id = 100;
Note : It will change the min salary to 100000 from 800000 for the job_id 100.
4> ALTER : Modify the structure of the table.
ALTER TABLE jobs ADD COLUMN dept_id INT;
5> DELETE : Remove Rows from the table
DELETE FROM jobs WHERE job_id = 100;
6> DROP : Removes the entire table.
DROP TABLE jobs;
7> TRUNCATE : Removes the all rows of the
table but it keeps the table structure
TRUNCATE TABLE jobs;
Note : > It removes all rows from the
table.
Ø It
retains the Table Structure
Ø It
is faster then DELETE statement
The "Dual Table" in Oracle
About Oracle Buffer
- All Commands of SQL are typed at the SQL prompt.
- Only one SQL statement is managed in the SQL buffer.
- The current SQL statement replaces the previous SQL statement in the buffer.
- The SQL statements can be divided into different lines within the SQL buffer.
- Only one line i.e, the current line can be active at a time in the SQL buffer.
- At SQL prompt, editing is possible only in the current SQL buffer line.
- Every statement of SQL should be terminated using semi colon ";".
- One sql statement can contain only one semi colon.
- To run the previous or current SQL statement in the bufer type "/" at SQL prompt.
- To open the SQL editor type "ED" at SQL prompt.
Introduction to SQL
- SQL usually pronounced as “Sequel” stands for Structured Query
Language.
- SQL is the native language of the Oracle Server. It is the language
used to communicate with the database. Again SQL consists of SQL
statements and SQL*Plus commands. SQL statements are used to talk to the
database. When you enter a SQL statement, it is stored in a part of the
memory called the SQL Buffer and remains there until a new statement is
entered. SQL*Plus is an Oracle tool that recognizes and submits
- SQL statements to the Oracle server for execution and contains its own
command language.
- It is very easy to write, it can be used by all kinds of users with
little or not programming experience.
- It is a non-procedural language.
- It reduces the amount of time required for creating and maintaining
systems.
- It is based on American National Standards Iinstitute (ANSI) standard
SQL.
- It manipulates data and tables definition in the database.
- It is entered into SQL buffer on one or more lines.
- It does not have a continuation character.
- It cannot be abbreviated.
- It uses a termination character to execute command immediately.
- Uses functions to perform some formatting.
- Recognizes SQL statements and sends them to the server.
- It accepts SQL input from files.
- It provides a line editor for modifying SQL statements.
- It controls environmental settings.
- Accesses local and remote databases.
- It doesn’t allow manipulation of values in the database.
- It is entered one line at a time and is not stored in the SQL buffer.
- Has a dash(-) as a continuation character if command exceed one line.
- Doesn’t require any termination character, commands are executed
immediately.
- It uses commands to format data.
- SQL is a high level language that provides a greater degree of
abstraction than procedural languages. It is designed so that the
programmer can specify what data is needed but need not specify how to
retrieves it.
- Applications written in SQL can be easily ported across systems.
- SQL as a language is independent of the way it is implemented
internally.
- The language even being simple and easy to learn can handle complex
situations.
- It is not just a query language, it can be used to define data
structures, control access to the data and delete, insert, modify occurrences
of the data.
- The results to be expected are well defined i.e, there is no ambiguity
about the way a query will interpret the data and produce the result.
- Querying data.
- Updating, inserting and deleting database objects.
- Controlling access to the database.
- Providing data integrity and consistency.
- Data Definition Language (DDL)
- Data Manipulation Language (DML)
- Transaction Control Lauguage (TCL)
- Data Control Language (DCL)
- CREATE statement.
- ALTER statement.
- DROP statement.
- RENAME statement.
- TRUNCATE statement.
- INSERT statement.
- UPDATE statement.
- DELETE statement.
- COMMIT statement.
- ROLLBACK statement.
- SAVEPOINT statement.
- GRANT statement.
- REVOKE statement.
Oracle Object Oriented Concepts
- All user defined data types are schema objects of the database.
- The user defined object data type can be used as Reference in other tables.
- All user defined data types and object are stored permanently in the Data dictionaries.
- USER_TYPES
- USER_OBJECTS
- We can query for the uesr defined data types and objects using the relational SELECT.
- The above statement create the user defined object data type called as Addr_type and PF_TYPE in the data dictionary called USER_TYPES.
- This data type is also called as collection in oracle, and this collection is reusable where ever the same data type collection is expected in project development.
- Once the user defined data types are created we can instantiate them in the normal relational tables.
- These instances look as normal attributes with in the table, but can be operated only with CONSTRUCTOR METHOD or OBJECT VIEWS.
- In any of the operation we have to provide reference to all attributes with in the instance, but partial association is not accepted.
- Is a table which is entirely build from the abstract type.
- To insert a record into an Object Table we may use the CONSTRUCTOR METHOD of the actual data type or directly implement the RELATIONAL INSERT statement.
- The Normal; INSERT or RELATION INSERT is possible only when the table does not contain any nested data types.
Order By Clause
- This clause in a select statement arranges the data on a specified column in ascending or in descending order.
- This clause will work after the execution of select statement.
- By default it arranges the data in ascending order.
- In order to arrange the data in descending order , an option called DESC is used.
- Data is arranged in order over a required column for temporary.
- Order by clause can be specified with one or more columns.