Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

SQL by Rohan

The document outlines basic SQL topics, including definitions of primary and foreign keys, SQL commands, data types, constraints, normalization, operators, and important SQL clauses. It provides examples of SQL statements for selecting, updating, deleting, and inserting data, as well as using various functions and joins. Additionally, it emphasizes the differences between database and DBMS, and offers resources for further learning.

Uploaded by

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

SQL by Rohan

The document outlines basic SQL topics, including definitions of primary and foreign keys, SQL commands, data types, constraints, normalization, operators, and important SQL clauses. It provides examples of SQL statements for selecting, updating, deleting, and inserting data, as well as using various functions and joins. Additionally, it emphasizes the differences between database and DBMS, and offers resources for further learning.

Uploaded by

rohannstu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Basic SQL Topics (Roadmap)

Best resource: https://www.w3schools.com/sql/sql_intro.asp

Primary key: Unique + Not Null


Candidate key: Unique

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

●​ DDL, DML, DCL, TCL, DQL


●​ What are commands under each of these categories and what each of these
commands actually do?

Data Types

●​ String data type like VARCHAR, TEXT etc


●​ Integer data type like INT, NUMBER etc.
●​ DATE
●​ FLOAT/DECIMAL
●​ BOOLEAN
●​ Also check out IDENTITY column (Auto Increment column)

Constraints:
●​ Primary key
●​ Foreign key
●​ Check constraints,Not null constraint, Unique constraint, default etc.
Normalisation in SQL

●​ Different normal forms like 1NF, 2NF, 3NF, BCNF

Operators

●​ Arithmetic operator
●​ Logical operator
●​ Comparison operator
●​ UNION, UNION ALL operator

CASE statement

●​ Simple case statement as well nested case statement


Important SQL clause
●​ DISTINCT clause
●​ ORDER BY clause
●​ LIMIT/TOP clause

Inner Join
●​ How to fetch data from multiple tables

Let’s start ……..

https://www.w3schools.com/sql/sql_intro.asp (just read the articles)

-​ 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

●​ SELECT - extracts data from a database


●​ UPDATE - updates data in a database
●​ DELETE - deletes data from a database
●​ INSERT INTO - inserts new data into a database
●​ CREATE DATABASE - creates a new database
●​ ALTER DATABASE - modifies a database
●​ CREATE TABLE - creates a new table
●​ ALTER TABLE - modifies a table
●​ DROP TABLE - deletes a table
●​ CREATE INDEX - creates an index (search key)
●​ DROP INDEX - deletes an index

Mytable:
Create table

SELECT Statement
Syntax:

For selecting a particular field or column:


SELECT CustomerName, City FROM Customers;

Returns all columns:


SELECT * FROM Customers;

Return all columns with unique and sorted way:


SELECT DISTINCT Country FROM Customers;

Returns how many unique countries are there:


SELECT COUNT(DISTINCT Country) FROM Customers;

Note: The COUNT(DISTINCT column_name) is not supported in Microsoft Access


databases.

Here is a workaround for MS Access:


SELECT Count(*) AS cnt
FROM (SELECT DISTINCT Country FROM Customers);

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 !

So various use of where is ,


SELECT * FROM Customers
WHERE CustomerID=1;

SELECT * FROM Customers


WHERE CustomerID > 80;

Etc…………!
Operator Description

= Equal

> Greater than

< Less than

>= Greater than or equal

<= Less than or equal


<> Not equal. Note: In some versions of SQL this operator
may be written as !=

BETWEEN Between a certain range

LIKE Search for a pattern

IN To specify multiple possible values for a column

<> :
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.

SELECT * FROM Products


ORDER BY Price;

DESC : For decreasing manners .


SELECT * FROM Products
ORDER BY Price, City DESC;

Using both ASC and DESC:

SELECT * FROM Customers


ORDER BY Country ASC, CustomerName DESC;

AND Operator
Used to return all the customers' names from the country Spain where the first letter is ‘G’.

SELECT * FROM Customers


WHERE Country = ‘Spain’ AND CustomerName LIKE ‘G%’;

MCQ:

SELECT * FROM Customers


WHERE Country = 'U' AND CustomerName LIKE 'G%';

Here ‘U’ is not a country. Does it provide errors?

Ans: No errors ! , Just it won’t show any value regarding the commands.

All conditions must be true !

SELECT * FROM Customers


WHERE Country = 'Germany'
AND City = 'Berlin'
AND PostalCode > 12000;
Combining AND and OR

With parentheses :

SELECT * FROM Customers


WHERE Country = 'Spain' AND (CustomerName LIKE 'G%' OR CustomerName LIKE 'R%');

Here all countries are Spain

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:

SELECT * FROM Customers


WHERE Country = 'Spain' AND (CustomerName LIKE 'G%' OR CustomerName LIKE 'R%');

Here it is considering other countries also.

OR Operator

SELECT * FROM Customers


WHERE City = 'Berlin' OR CustomerName LIKE 'G%' OR Country = 'Norway';

Not Operator
Used to abstain from some particular thing.

SELECT * FROM Customers


WHERE NOT Country = 'Spain';

SELECT * FROM Customers


WHERE City NOT IN ('Paris', 'London');
INSERT INTO STATEMENT

INSERT INTO (‘Comlumn name1, Column name2…..)


VALUES (‘rohan’, ‘romio’.....);

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:

Select CustomerName, Address where Address IS NULL;

UPDATE Statement

UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;

Here,
Customer -> table name
ContactName -> column name

DELETE Statement

For deleting any specific record in the table:

DELETE FROM table_name WHERE condition;


Ex:
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';

For deleting the whole record in the table without deleting the
whole table:

DELETE FROM tablename;


Ex: DELETE FROM Customers;

TOP / LIMIT

TOP - Not supported for all database;

Select top 3 *from table_name;

LIMIT - For mysql it will select a particular portion;

Select *from table_name


Limit 3;

MIN() and MAX(); function

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

Find the total number of rows in the Products table:

SELECT COUNT(*)
FROM Products;

Syntax:
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
How many different prices are there in the Products table:

SELECT COUNT(DISTINCT Price)


FROM Products;

Name the column "Number of records":

SELECT COUNT(*) AS [Number of records]


FROM Products;

SUM / AVG (functions)

Syntax;
Select SUM(column_name)
From table_name
Where condition;

Select SUM(*) not recognized;


Note : all statements will be at first and while performing function
we’ve to select that particular function.

Like

Select *from table_name


Where columnName Like ‘firsttowletter%’;

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)

It returns all the customers have an order in the order table

JOIN / INNER JOIN


select a.name, b.resturant from MyInfo a inner join Resturants b
on a.OrderNO = b.OrderNo;

With aliasing a and b .

It basically display all the common information between two table.

MyInfo table

Restaurants table

If this are tow table inner join will provide

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.

You might also like