0% found this document useful (0 votes)
14 views11 pages

SQL Extra

Uploaded by

gnumber019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views11 pages

SQL Extra

Uploaded by

gnumber019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Sql

ALTER TABLE (Modifying Tables)


ALTER TABLE table_name
ADD column_name datatype;
Change coloumn name
ALTER TABLE table_name
ALTER COLUMN column_name new_datatype;

Add primary key to a relation


ALTER TABLE table_name ADD PRIMARY KEY (Coloumn name);

Add foreign key to a relation


ALTER TABLE table_name ADD FOREIGN KEY(attribute name)
REFERENCES referenced_table_name (attribute name);
Add constraint UNIQUE to an existing
attribute
ALTER TABLE table_name ADD UNIQUE (attribute name);

Add an attribute to an existing table


ALTER TABLE table_name ADD attribute name DATATYPE;

Modify datatype of an attribute


ALTER TABLE table_name MODIFY attribute DATATYPE;

Modify constraint of an attribute


ALTER TABLE table_name MODIFY attribute DATATYPE NOT NULL;

Remove an attribute
ALTER TABLE table_name DROP attribute;
Remove primary key from the table
ALTER TABLE table_name DROP PRIMARY KEY;
Renaming of columns
ALTER TABLE table_name
RENAME COLUMN old_column_name TO new_column_name;

WHERE Clause
The WHERE clause filters rows based on specified conditions within a
SQL query.
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Ex:
SELECT *
FROM employees
WHERE salary > 50000;
ORDER BY Clause
The ORDER BY clause is used to sort the result set of a query either
in ascending (default) or descending order based on one or more
columns.
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
Ex: SELECT emp_name, salary
FROM employees
ORDER BY salary DESC;

We can also use ORDER BY and WHERE clause together like :


SELECT emp_name, salary
FROM employees
WHERE salary > 50000
ORDER BY salary DESC;
Substring pattern matching
In SQL, substring pattern matching refers to finding or extracting parts
of a string that match a specific pattern. This is commonly achieved
using various string functions and operators available in SQL.
Pattern Matching Functions and Operators:
1. LIKE Operator:
The LIKE operator is used to match patterns in a string column. It
allows the use of wildcards:
% (percent sign): Represents zero or more characters.
_ (underscore): Represents a single character.
Example:
Let's say you want to retrieve all names from the employees table that
start with the letter 'J':

SELECT emp_name
FROM employees
WHERE emp_name LIKE 'J%';
2. SUBSTRING() Function:
The SUBSTRING() function extracts a substring from a string.
Example:
If you want to extract a part of a string from the description column
starting at position 3 and including the next 5 characters:

SELECT SUBSTRING(description, 3, 5) AS extracted_text


FROM your_table;
Operations of Relation in
Sql
In SQL, operations on relations (tables) are fundamental for manipulating
and querying data within a relational database. Some common
operations include:
1. Selection
The selection operation retrieves rows from a table that satisfy a specified
condition using the SELECT statement and the WHERE clause.
Example:
Retrieve all rows from the employees table where the department is 'Sales':
SELECT *
FROM employees
WHERE department = 'Sales';
2. Projection
Projection retrieves specific columns from a table using the SELECT
statement to focus only on the required columns.
Example:
Retrieve only the emp_name and salary columns from the employees table:
SELECT emp_name, salary
FROM employees;
3. Union
The UNION operator combines the result sets of two or more SELECT statements, removing
duplicates by default.
Example:
Combine the results of two queries into a single result set:

SELECT emp_name
FROM employees
WHERE department = 'Sales'
UNION
SELECT emp_name
FROM employees
WHERE department = 'Marketing';

4. Intersection
The INTERSECT operator returns the common rows between the result sets of two or more SELECT
statements.
Example:
Retrieve the employees who are in both 'Sales' and 'Marketing' departments:

SELECT emp_name
FROM employees
WHERE department = 'Sales'
INTERSECT
SELECT emp_name
FROM employees
WHERE department = 'Marketing';
5. Difference
The EXCEPT or MINUS operator returns rows from the first query that
are not present in the result set of the second query.
Example:
Retrieve employees who are in 'Sales' but not in 'Marketing':
SELECT emp_name
FROM employees
WHERE department = 'Sales'
EXCEPT
SELECT emp_name
FROM employees
WHERE department = 'Marketing';
6. Join
Joins combine rows from two or more tables based on a related column
between them.
Example:
Retrieve information from two tables (employees and departments)
based on a common column (department_id):
SELECT e.emp_name, d.department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id;
 Write all the single row, multiple row ,
aggregate functions ,description and their
examples with output from page no
158,161,162,164,166.

You might also like