0% found this document useful (0 votes)
7 views

My SQL Prog

Uploaded by

sirius black
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

My SQL Prog

Uploaded by

sirius black
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Create the employees table


CREATE TABLE employees (

id INT PRIMARY KEY,

name VARCHAR(50),

age INT,

salary DECIMAL(10, 2),

department_id INT

);

2.Insert data into the employees table


INSERT INTO employees (id, name, age, salary, department_id)

VALUEs

(1, 'John', 35, 60000.00, 1),

(2, 'Jane', 28, 55000.00, 2),

(3, 'Michael', 40, 75000.00, 1),

(4, 'Emily', 32, 62000.00, 2),

(5, 'David', 45, 80000.00, 3);

3.Selecting Specific Columns


SELECT name, age FROM employees;

4.Filtering with WHERE Clause:


SELECT * FROM employees WHERE age > 30;

5.Sorting with ORDER BY


SELECT * FROM employees ORDER BY salary DESC;
6.Aggregation with GROUP BY:
SELECT department_id, AVG(salary)

FROM employees

GROUP BY department_id;

7.Filtering Aggregated Data with


HAVING:
SELECT department_id, AVG(salary)

FROM employees

GROUP BY department_id

HAVING AVG(salary) > 60000;

R elational operators, also known as comparison operators, are used in SQL (Structured Query
Language) to compare values and determine relationships between them. These operators
are often used in the WHERE clause of SQL queries to filter or select specific rows based on
certain conditions. Here are the common relational operators:

1.Equal To (=):

Checks if two values are equal.

SELECT * FROM employees WHERE age = 30;

2. Not Equal To (<>or !=):

Checks if two values are not equal.

SELECT * FROM employees WHERE department_id<> 2;

3. Greater Than (>), Greater Than or Equal To (>=):

Checks if one value is greater than or equal to another.


SELECT * FROM employees WHERE salary > 50000;

4. Less Than (<), Less Than or Equal To (<=):

Checks if one value is less than or equal to another.

SELECT * FROM employees WHERE age <= 40;

5. BETWEEN ... AND ...:

Checks if a value falls within a specified range (inclusive).

SELECT * FROM employees WHERE age BETWEEN 25 AND 35;

6. NOT BETWEEN ... AND ...:

Checks if a value falls outside a specified range.

SELECT * FROM employees WHERE salary NOT BETWEEN 60000 AND 80000;

7. IN:

Checks if a value matches any value in a list.

SELECT * FROM employees WHERE department_id IN (1, 3);

8. NOT IN:

Checks if a value does not match any value in a list.


SELECT * FROM employees WHERE age NOT IN (25, 30, 35);

9. LIKE:

Used to match a pattern in a string value. "%" is a wildcard that matches any number of characters,
and "_" is a wildcard that matches a single character.

SELECT * FROM employees WHERE name LIKE 'J%';

10. NOT LIKE:

Checks if a value does not match a specified pattern.

SELECT * FROM employees WHERE name NOT LIKE 'A%';

You might also like