SQL
SQL
KEYS
Foreign Key:
A foreign key is a column within a table that refers to (or "relates to") a
unique value in a referenced table. Each value in the foreign key column
must have a matching value in the referenced table.
Primary Key VS Foreign Key
S.no. Primary key Foreign key
• CREATE database
Syntax : CREATE database <databasename>;
• USE database
Syntaax : USE parkavi;
• CREATE TABLE
Syntax: CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any
position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the
second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a"
and are at least 2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a"
and are at least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a"
and ends with "o"
IS NULL Vs IS NOT NULL
IS NULL
IS NOT NULL
Relational operators
• To compare 2 values relational operators are used.
• True / false state
Relational operators used in sql
=,>,<,>=,<=,<> (not equal to)
Logical operators
OR
• OR (II)
• AND (&&)
• NOT(!)
AND
NOT
SQL Constraints
• NOT NULL- Ensures that the column can not have null value.
• UNIQUE –Ensures that all values in a column are different.
• PRIMARY KEY-used to uniquely identify a row in the table.
• Example NOT NULL
• CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
UNIQUE command
Syntax:
Syntax:
Natural join
• The SQL NATURAL JOIN is a type of EQUI JOIN and is structured in
such a way that, columns with the same name of associated tables
will appear once only.
• Natural Join: Guidelines
• - The associated tables have one or more pairs of identically named
columns.
- The columns must be the same data type.
Syntax:
SELECT * FROM table1 NATURAL JOIN table2
SQL commands are mainly categorized into five categories:
• CREATE: This command is used to create the database or its objects (like
table, index, function, views, store procedure, and triggers).
• DROP: This command is used to delete objects from the database.
• ALTER: This is used to alter the structure of the database.
• TRUNCATE: This is used to remove all records from a table, including all
spaces allocated for the records are removed.
• COMMENT: This is used to add comments to the data dictionary.
• RENAME: This is used to rename an object existing in the database.
List of DML commands:
SYNTAX:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|
DESC;
What is an Aggregate Function in SQL?
• An aggregate function in SQL returns one value after
calculating multiple values of a column. We often use
aggregate functions with the GROUP BY and HAVING
clauses of the SELECT statement.
• Various types of SQL aggregate functions are:
Count()
Sum()
Avg()
Min()
Max()
Order By Two
Columns in SQL
SYNTAX:
SELECT column_name(s)
FROM table_name
GROUP BY column_name(s)
GROUP BY.. SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
↓
ORDER BY column_name(s);
HAVING WITH ORDER BY
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;
retrieve all records from the
CUSTOMERS table where the
sum of their salary is less than
4540, ordered by their name in
ascending order
SELECT NAME, SUM(SALARY)
as total_salary
FROM CUSTOMERS
GROUP BY NAME
HAVING SUM(SALARY) < 4540
ORDER BY NAME
MAX() function
on date
To get data of 'ord_num',
'ord_amount', 'ord_date',
'agent_code' from the
'orders' table with the
following conditions -
output:
{'google', 'microsoft', 'apple', 'cherry', 'banana'}