SQL by Rohan
SQL by Rohan
Foreign key:
If there are two tables then one table's primary key is another table’s foreign key.
Foreign key refers to another table’s primary key.
What is RDBMS ?
● How data is stored in a relational database?
● What is schema write to relational database?
SQL Commands
Data Types
Constraints:
● Primary key
● Foreign key
● Check constraints,Not null constraint, Unique constraint, default etc.
Normalisation in SQL
Operators
● Arithmetic operator
● Logical operator
● Comparison operator
● UNION, UNION ALL operator
CASE statement
Inner Join
● How to fetch data from multiple tables
- SQL keywords are NOT case sensitive: select is the same as SELECT
- Database systems require a semicolon at the end of each SQL statement ( Yes ).
Note: Database and DBMS are not the same. DBMS is basically a software which is
used to do CRUD operations in a database.
Data types -
Some of The Most Important SQL Commands
Mytable:
Create table
SELECT Statement
Syntax:
Where clause
SELECT * FROM Customers
WHERE Country='Mexico';
So it will return basically all the record of the table where Mexico presents.
We can also select particular field by condition rather returning the whole table:
Select ContactName, City, Country from Customers
Where Country = 'Mexico';
Note: The WHERE clause is not only used in SELECT statements, it is also used in UPDATE,
DELETE, etc.!
Text fields vs numeric fields is that text fields include single quotation ( sometimes
double in other databases ) and numeric doesn’t allow quotation !
Etc…………!
Operator Description
= Equal
<> :
SELECT *From Products
WHERE Price <> 18;
Output:
BETWEEN :
SELECT * FROM Products
WHERE Price BETWEEN 50 AND 60;
LIKE:
SELECT *FROM Customers
WHERE City LIKE ‘s%’;
‘X%’ represents every city’s first letter and will be X, Where X could be any alphabet !
IN :
SELECT * FROM Customers
WHERE City IN ('Paris','London');
ORDER BY keyword
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
AND Operator
Used to return all the customers' names from the country Spain where the first letter is ‘G’.
MCQ:
Ans: No errors ! , Just it won’t show any value regarding the commands.
With parentheses :
Without parentheses:
Without parenthesis, the select statement will return all customers from Spain that starts with
a "G", plus all customers that starts with an "R", regardless of the country value:
OR Operator
Not Operator
Used to abstain from some particular thing.
NULL VAlUES
A NULL value is different from a zero value or a field that contains spaces. A field with a
NULL value is one that has been left blank during record creation!
It is not possible to test for NULL values with comparison operators, such as =, <, or <>.
We will have to use the IS NULL and IS NOT NULL operators instead.
Syntax:
UPDATE Statement
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
Here,
Customer -> table name
ContactName -> column name
DELETE Statement
For deleting the whole record in the table without deleting the
whole table:
TOP / LIMIT
Syntax:
select MIN(price) as [giving column name like MinimumID in ex]
from tablename;
Like:
select MIN(teacherid) as Minimum_ID
from teachers;
Likewise MAX()...............
Count function
SELECT COUNT(*)
FROM Products;
Syntax:
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
How many different prices are there in the Products table:
Syntax;
Select SUM(column_name)
From table_name
Where condition;
Like
In operator
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
Ex:
Select *from customer
where customerID IN (select customerID from Orders)
MyInfo table
Restaurants table
Left join:
select a.Resturant, b.name from resturants a
left join MyInfo b on a.OrderNO = b.OrderNO;
It will provide all values from left and all unique from right
Right join:
Vice versa of left join.