SQL
Structured Query Language.
Creating a database
Modifying data
Storing data
FIRST LINE OF CODE:
mysql-ctl cli; here, ctl ----------control transaction durability and cli--------command line interface.
Database:
Create database <name>;
Use <name>;
To show database---------- show databases;
To drop database:
Drop database <name>;
Tables:
Create table table_name
(column_name datatype,
Column_name datatype);
To insert data in the table ------------------
Insert into <name>
Values(data,data,…,.,.,.,,.,.);
PRIMARY KEY:
A column that is common in two or more tables is an primary key. Like
[Link] is common in both the employee name data and the salary the achieved table.
Create table employees
(id int primary_key, name varchar(50));
AUTO INCREMANT:
Create table employees
(id int primary_key auto_increment, name varchar(50));
CRUD:
Create:
Creating tables and inserting data.
READ:
SELECT COMMANDS WITH CONDITIONS AND WHERE CLAUSE.
SELECT ID, NAME FROM <TABLE NAME>;
SELECT NAME , STATUS FROM EMPLOYEES WHERE STATUS = ‘EMPLOYED’
A LIAS:(as)
It used to temporary rename the column names.
Select id as ‘emp_id’, name as ‘employee name’ in employees;
UPDATE:
Syntax:
update <table name> set <column name> = <to be update> where condition.
Here, update, set, and where are used to change the data permanently.
DELETE:
Delete from <name>
Where <condition>;
STRING OPERATIONS:
1. CONCATE:
It is used to join two rows or variables.
It uses with select clause, makes temporary changes.
Select concate (column_name ‘ ‘ column_name) as <name>\
From <table name>;
2. CONCAT_WS:
No need to give spaces in between strings.
SELECT CONCAT_WS (' ', first_name, last_name) AS full_name
FROM employees;
3. SUBSTRING:
It is helpful deriving substrings from original string.
“substr” is the keyword.
Select substr(‘Hello world’, 1,4);
Concat with substring:
Select concat_ws(substr(title, 1, 4), ‘ ‘, ‘is published by’, published_by) as datas from books;
4. REPLACE:
case sensitive
select replace(‘Hello world’ ‘Hell’ ‘*766’) as new name; opt *766o world.
Replace function with concat and substring.
select concat(substr(replace(title,'e','3'), 1, 10), ' ', 'was written by',' ', author_fname,' ',
author_1name)as datas from books;
[Link]:
Select reverse(author_fname) as result from books;
PALINDROME:
select concat(author_fname,reverse(author_fname)) as result from books;
6. CHARACTER LENGTH:
SELECT CHAR_LENGTH('Hello, World!') AS string_length;
[Link]/ LOWER:
select upper(name) from books;
SELECTION REFINING:
DISTINCT KEYWORD:
It is used to filter out duplicate rows in a column or data.
SYNTAX:
Select distinct author_1name from books;
DISTINCT using concate:
Syntax:
Select distinct(concate(author_fname , ‘ ‘, author_1name)) as full_name from books;
Select distinct author_fname, author_1name from books;
ORDER BY:
It makes data in columns in aesc or desc order.
Default aesc for desc tou have to declare it.
Syntax:
Select <column names> from <table name> order by <column name to be orderd>; you can
also declare by indexes like in th space of <> you can give indexes like1,2.
LIMIT:
It is used with mostly order by to give limit in output.
Syntax:
Select title from books limit 5;
Select title, author_fname, released_year from books order by released_year desc limit 5;
It gives 5 most recent books.
LIKE:
It is helpful in searching in data with only a thing that is in the sentence, meaning if you
remember only part of sentence then also you can search the element.
It uses % and _ to search in the string.
Syntax:
Select title, author_fname from books where author_name like %da%;
AGREEGATE FUNCTIONS:
COUNT:
It helps in counting no of values are present.
Syntax:
Select count(*) title from books;
Select count(title) from books;
MIN/MAX:
Gives min and max of integer values using min and max
Select min(released_year) from books;
SELECT department,
MIN(salary) AS min_salary,
MAX(salary) AS max_salary
FROM employees
GROUP BY department;
TIME STAMP FUNCTIONS:`
select current_date();
select current_time();
select current_timestamp(), current_date(),current_time();
select date(current_timestamp());
select time(current_timestamp());
select time();
select year(current_timestamp());
select month(current_timestamp());
select day(current_timestamp());
select hour(current_timestamp());
SELECT MINUTE(CURRENT_TIMESTAMP());
SELECT SECOND(CURRENT_TIMESTAMP());
EXTRACT KEY WORD:
SYNTAX:
SELECT EXTRACT(COMPONENT FROM DATETIME_EXPRESSION);
DATEDIFF:
SYNTAX:
SELECT DATEDIFF(DATE1, DATE2); DATE1> DATE2.
TIMEDIFF:
SYNTAX:
SELECT TIMEDIFF(TIME1, TIME2);
DATE_ADD:
SYNTAX:
SELECT DATE_ADD(DATE, INTERVAL VALUE INTERVAL_UNIT);
LOGICAL OPERATORS:
NOT EQUAL:
select title, released_year from books where released_year != '2012' ;
NOT LIKE:
select title, released_year from books where title NOT like 'W%';
GREATER THAN:
select title, released_year from books where released_year < '1980';
LESS THAN:
select title, released_year from books where released_year > '1980';
AND:
select title, released_year, author_1name from books where author_1name = 'lahiri' and
released_year < '2017';
OR:
select title, author_1name from books where author_1name = 'eggers' or 'chabon';
IN/NOT IN:
SELECT column_name(s) FROM table_name WHERE column_name IN (value1, value2, ...);
BETWEEN:
select title, pages from books where pages between 100 and 200;
CASE ELSE:
select title, author_1name,
case
when title like '%stories%' then 'Short Stories'
when title = 'just kids' or title = ' A Heartbreaking Work of Staggering Genius' then 'Memories'
else 'Novel'
end as type
from books;
RELATIONSHIP:
ONE TO ONE
ONE TO MANY
MANY TO MANY
FOREIGN KEY:
A column that is common between two tables is a foreign key.
FOR EXAMPLE:
Create table customers (
Id int not null auto increment primary key,
First_name varchar (100),
Last_name varchar(100),
E-mail varchar (100)
);
Create table orders (
Id int not null auto increment primary key,
Order varchar (100),
Price decimal (8, 2),
Customer_id int,
Foreign key (customer_id) references customers(id);
);
INNER JOIN:
Joins to tables based on common column name.
Select * from customers
Join orders
On [Link] = orders.customer_id;
LEFT JOIN:
select * from customers
left join orders
on [Link] = orders.customer_id;
RIGHT JOIN:
select * from customers
right join orders
on [Link] = orders.customer_id;