You are on page 1of 7

SQL:

Database(DB):
Any collection of related information :Phonebook,todo list,your 5 best
friends,facebook's User base ,shopping list

Databases can be stored in different way : On paper,in your mind, on a


computer,powerpoint,comment Section

Computer + Databases = <3

Amazon.com : Keep tracks of products,Reviews ,Purchase orders,


Trillions of pieces of information need to be stored and readily available.
information is extremely valuable and critical to amazon.com 's functioning

Shopping list : keep tracks of consumer products that nedd to be


purchased ,security is not imp because it is written in paper. 10 -20 things store
the product

so computer +databases is Heart for large amount of data and information

DBMS : A special software program that helps users create and maintain a database
*Makes it easy to manage large amount of information
*Handles Security
*Backups your data
*interacts with software applications
*importing and exporting data

Amazon ------------>>>> DBMS


<<<<<-------
Amazon.com will interact with the DBMS in order to create,read, update, and delete
information C .R .U .D

Two types of Databases


1)Relation databases (SQL) : *Orgainze data into one or more tables
*Each table has Columns and rows
*A unique key identifies each row
*mySQL,Oracle,postgreSQL etc...

2)Non - Relational(noSQL /not just SQL) : * Organize data is anything but a


traditional table
*key-value stores
*Document (JSON,XML,etc)
*Graphs
*Flexible Tables
*mongoDB,dynamoDB,firebase,apache cassandra ,etc
*Most NRDBMS will implement their own language for performing CRUD and
administrative operations on the database.

Relational Databases ma use thay(SQL):

SQL : Structured Query Language


*interacting with RDBMS
*Used to perform CRUD operatin as well as other administrative tasks (user
management ,security,backup,etc)
*used to define tables and structures
*SQL code used on one RDBMS is not always portable to another without modification
Database Queries :
*Queries are requests made to the database management system for specific
information
*As the database's structure become more and more complex,it becomes more difficult
to get the specific pieces of information we want

*A google search is a query

Table ma data store thay relational db ,sql


row = individual entry ,column=vertical
Studend_id = primary key ,uniquely identified by row

emp_ssn=social security number = 123456789,222222222, ..natural key


emp =1,2,3,4..key is sarogacy key
natural key related to real world
both are primary key

Foreign key :attribut which can link another table


fk store primary key of a row in antoher database table

composite key = two key are fk .

Not all rdbms follow the sql slice difference

SQL : is actually a hybrid language , its basically 4 types of languages in one


1)DQL (Data Query Language) : *Used to query the database for information
*Get information that is alread stored there

2)DDL(Data Definition Language) :Used for defining database schemas

3)Data Control Language(DCL):*Used for controlling access to the data in the


database
*user & permission management

4)Data Manipulation Language(DML) : *Used for inserting ,updating and deleting data
from database

Queries : A Query is a set of instruction given to the RDBMS (written is SQL) that
tell the RDBMS what information you want it to retrieve for you.
*Tons of data in a DB
*Often hidden in a complex schema
*Goal is to only get the data you need

All are datatypes :


INT ----- whole Numbers
DECIMAl (M,N) like(10,4) ----- Decimal number -exact value
VARCHAR () ----- String of text of length
BLOB -----Binary Large object,Stores large data
DATE ----- 'YYYY-MM-DD '
TIMESTAMP ------ 'YYYY-MM-DD HH:MM:SS' used for recording

in Sql we have to write all queries in capital letters


any command in Sql ends with semicolumn ;
attribute or name is small letter

CREATE TABLE student (


Column Name Data type ,
student_id INT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
major VARCHAR(20) UNIQUE ,or aaa badha ma DEFAULT 'undecided',

PRIMARY KEY(student_id)
);

table ni ander shu che ae jova mate


DESCRIBE student;

delete table mate :


DROP TABLE student;

update mate
ALTER TABLE student ADD gpa DECIMAL(3,2);

remove column mate :


ALTER TABKE student DROP COLUMN gpa;

table ma data insert karva mate


INSERT INTO student VALUES(1,'Karan','Maths');

SELECT * FROM student;aako data aavi jay aa query thi

INSERT INTO student VALUES(2,'MEET','CA');

you have to specific entery mate use thay like koi column ma entery na hoy te na
mate here major nathi so ana mate null aave.
INSERT INTO student(student_id,name) VALUES(3,'kathan');

INSERT INTO student VALUES(4,'Riddhi','Biology');

INSERT INTO student VALUES(5,'Karan','Maths');

mare name null na joietu hoy to NOT NULL use karvanu column ma first
UNIQUE bhi lakiye to particular column unique
starting ma lakhvanu

this are called CONSTRAINTS in sql ..

DEFAULT 'undecided',
primary key ni jagyae Auto_INCREMENT,
lakiye to jate key increment thaya kare. and also insert values db ma lakhvu na
pade specifically .

Updating and Deleting table :

DELETE FROM student;

DELETE FROM student


WHERE student_id = 4;

DELETE FROM student


WHERE major = 'Sociology' AND name = 'Kate';

UPDATE student
SET major = 'Undecided';

UPDATE student
SET name = 'Johnny'
WHERE student_id = 4;

UPDATE student
SET major = 'Biological Sciences'
WHERE major = 'Biology';

UPDATE student
SET major = 'Biosociology'
WHERE major = 'Biology' OR major = 'sociology'

UPDATE student
SET major = 'Undecided', name = 'Tom'
WHERE student_id = 4;

Basic Queries :
SELECT *
FROM student;

SELECT student.name, student.major


FROM student;

SELECT *
FROM student
WHERE name = 'Jack';

SELECT *
FROM student
WHERE student_id > 2;
operation : <> means not equal ,<,>,AND,OR,>=

SELECT *
FROM student
WHERE major <> 'Biology';

SELECT *
FROM student
WHERE major = 'Biology' AND student_id > 1;

SELECT student.name, student.major


FROM student; aama specific column display karavi hoy ana mate

SELECT student.name, student.major


FROM student
ORDER BY name DESC;

alpabatically order ma name print thay . bydefault assending order ma print thay
but if you want to print descending order then you have to mention DESC;or ASC

SELECT student.name, student.major


FROM student
ORDER BY major,student_id ASC;
so major is asc and also student_id because je aapde be duplicate lidha che anathi
aapdne kahaber pade che

SELECT student.name, student.major


FROM student
LIMIT 2; means only 2 rows j display thase

SELECT *
FROM student
WHERE name IN ('Karan','Meet','Darshna');

so means table ni ander aa 3 na name hoy to data aave .jenu name hoy ano data
display thay

IN company db when we create first table that time we cannot declare fk later we
can declare.

whenever we create fk that time also REFERENCES we have to write ON DELETE (CASCADE
OR SET NULL.)
FOREIGN KEY(mgr_id) REFERENCES employee(emp_id) ON DELETE SET NULL

Queries for company database :

SELECT *
FROM employee;

-- Find all clients


SELECT *
FROM clients;

-- Find all employees ordered by salary


SELECT *
from employee
ORDER BY salary ASC/DESC;

-- Find all employees ordered by sex then name


SELECT *
from employee
ORDER BY sex, name;

-- Find the first 5 employees in the table


SELECT *
from employee
LIMIT 5;

-- Find the first and last names of all employees


SELECT first_name, employee.last_name
FROM employee;

-- Find the forename and surnames names of all employees


SELECT first_name AS forename, employee.last_name AS surname
FROM employee;

-- Find out all the different genders


SELECT DISCINCT sex
FROM employee;
-- Find all male employees
SELECT *
FROM employee
WHERE sex = 'M';

-- Find all employees at branch 2


SELECT *
FROM employee
WHERE branch_id = 2;

-- Find all employee's id's and names who were born after 1969
SELECT emp_id, first_name, last_name
FROM employee
WHERE birth_day >= 1970-01-01;

-- Find all female employees at branch 2


SELECT *
FROM employee
WHERE branch_id = 2 AND sex = 'F';

-- Find all employees who are female & born after 1969 or who make over 80000
SELECT *
FROM employee
WHERE (birth_day >= '1970-01-01' AND sex = 'F') OR salary > 80000;

-- Find all employees born between 1970 and 1975


SELECT *
FROM employee
WHERE birth_day BETWEEN '1970-01-01' AND '1975-01-01';

-- Find all employees named Jim, Michael, Johnny or David


SELECT *
FROM employee
WHERE first_name IN ('Jim', 'Michael', 'Johnny', 'David');

Functions in SQL :
block of the code ,avg,counting ,....

-- Find the number of employees


SELECT COUNT(super_id) (how many employees are inside employee
table)
FROM employee;

-- Find the average of all employee's salaries


SELECT AVG(salary)
FROM employee;

-- Find the sum of all employee's salaries


SELECT SUM(salary)
FROM employee;

Aggregation :

-- Find out how many males and females there are


SELECT COUNT(sex), sex
FROM employee
GROUP BY sex

-- Find the total sales of each salesman


SELECT SUM(total_sales), emp_id
FROM works_with
GROUP BY client_id;

-- Find the total amount of money spent by each client


SELECT SUM(total_sales), client_id
FROM works_with
GROUP BY client_id;

WildCards:koi specific find karvu hoy ana mate use thay che
(-- % = any # characters), (_ = one character)
this is pattern symbol

-- Find any client's who are an LLC


SELECT *
FROM client
WHERE client_name LIKE '%LLC'; here %means any no of character LLC aave ae
data display thay

-- Find any branch suppliers who are in the label business


SELECT *
FROM branch_supplier
WHERE supplier_name LIKE '% Label%';

-- Find any employee born on the 10th day of the month


SELECT *
FROM employee
WHERE birth_day LIKE '_____10%'; _means one character here 4 desh lidhi che so
whole year aavi jay october month

-- Find any clients who are schools


SELECT *
FROM client
WHERE client_name LIKE '%Highschool%';

UNIONS : is basically a special sql which we can use to combine the results of
multiple select statements into one .

JOIN : different tabls into single result

As lakhvathi aapde name change kari shakiye


select SUM(id) as "Total Salary" from student;

so here SUM(id) rename to Total Salary

On Delete set null : : ma je id or name aapau hoy te null thay che like if i say
id then only is null in fk table
but in

On Delete Cascade : ma aakhi row j delete thai jay che ae row no badho data delete
thai jay che sathe fk table ma bhi hoy to ama bhi aakhi row delete thai jay che..

You might also like