Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

-- CRUD OPERATION --

-- CREATE –
-- create a database shirts_db –
create database shirts_db;
use shirts_db;
select database();
-- create a table shirts --
create table shirts(
shirts_id int not null auto_increment primary key,
article varchar(30) ,
color varchar(20) ,
shirt_size varchar(10) ,
last_worn int
);
--------------------------------------------------------------------------------------------------------------------------------------

-- insert values --
INSERT INTO shirts(article, color, shirt_size, last_worn)
VALUES ('t-shirt', 'white', 'S', 10),
('t-shirt', 'green', 'S', 200),
('polo shirt', 'black', 'M', 10),
('tank top', 'blue', 'S', 50),
('t-shirt', 'pink', 'S', 0),
('polo shirt', 'red', 'M', 5),
('tank top', 'white', 'S', 200),
('tank top', 'blue', 'M', 15);
-- insert values separately –
INSERT INTO shirts(article, color, shirt_size, last_worn) VALUES
('polo shirt', 'purple', 'M', 50);

--------------------------------------------------------------------------------------------------------------------------------------

-- READ –
+ show tables;
+ desc shirts;
+ select * from shirts;
+ select article, color from shirts;
+ select shirts_id from shirts
where shirt_size='M';
--------------------------------------------------------------------------------------------------------------------------------------

-- UPDATE --

+ UPDATE shirts SET shirt_size='L'


WHERE article='polo shirt';
+ UPDATE shirts SET last_worn=0
WHERE last_worn=15;
+ UPDATE shirts SET shirt_size='XS', color='off white' where color='white';
--------------------------------------------------------------------------------------------------------------------------------------
-- DELETE –

+ DELETE from shirts


where last_worn >= 200;
+ DELETE FROM shirts
where article='tank top';
+ DELETE from shirts;
+ DROP table shirts;
+ ALTER TABLE shirts DROP COLUMN color;
--------------------------------------------------------------------------------------------------------------------------------------

# (TO ADD A COLUMN)


ALTER TABLE table_name ADD COLUMN column_name data_type (definition if necessary);

# (TO ADD A VALUES TO A PARTICULAR ROW / UPDATE A COLUMNS PARTICULAR DATA)


UPDATE table_name SET column_name = CONCAT(column_name, ', new_value1, new_value2')
WHERE id = 1; (OR) WHERE name = row_name;

# (ALTER A COLUMN SPECIFICATIONS)


ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT default_value;

# (TO DELETE A COLUMN)


alter table table_name drop column column_name;

#(TO RENAME A COLUMN )


(-- temporary assigning --)
select email AS e_mail from column_name;

(-- permanent assigning --)


ALTER TABLE company1 CHANGE e_mail email VARCHAR(50);

#(to read)
+ SHOW COLUMNS FROM column_name FROM data_base_name;
+ SHOW INDEX FROM column_name FROM data_base_name;
--------------------------------------------------------------------------------------------------------------------------------------

BOOK SHOPPING
CREATE DATABASE book_market;
USE book_market;
CREATE TABLE books
(
book_id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(100),
author_fname VARCHAR(100),
author_lname VARCHAR(100),
released_year INT,
stock_quantity INT,
pages INT,
PRIMARY KEY(book_id)
);
INSERT INTO books (title, author_fname, author_lname, released_year, stock_quantity, pages)
VALUES('The Namesake', 'Jhumpa', 'Lahiri', 2003, 32, 291),
('Norse Mythology', 'Neil', 'Gaiman',2016, 43, 304),
('American Gods', 'Neil', 'Gaiman', 2001, 12, 465),
('Interpreter of Maladies', 'Jhumpa', 'Lahiri', 1996, 97, 198),
('A Hologram for the King: A Novel', 'Dave', 'Eggers', 2012, 154, 352),
('The Circle', 'Dave', 'Eggers', 2013, 26, 504),
('The Amazing Adventures of Kavalier & Clay', 'Michael', 'Chabon', 2000, 68, 634),
('Just Kids', 'Patti', 'Smith', 2010, 55, 304),
('A Heartbreaking Work of Staggering Genius', 'Dave', 'Eggers', 2001, 104, 437),
('Coraline', 'Neil', 'Gaiman', 2003, 100, 208),
('What We Talk About When We Talk About Love: Stories', 'Raymond', 'Carver', 1981, 23, 176),
("Where I'm Calling From: Selected Stories", 'Raymond', 'Carver', 1989, 12, 526),
('White Noise', 'Don', 'DeLillo', 1985, 49, 320),
('Cannery Row', 'John', 'Steinbeck', 1945, 95, 181),
('Oblivion: Stories', 'David', 'Foster Wallace', 2004, 172, 329),
('Consider the Lobster', 'David', 'Foster Wallace', 2005, 92, 343);

INSERT INTO books


(title, author_fname, author_lname, released_year, stock_quantity, pages)
VALUES ('10% Happier', 'Dan', 'Harris', 2014, 29, 256),
('fake_book', 'Freida', 'Harris', 2001, 287, 428),
('Lincoln In The Bardo', 'George', 'Saunders', 2017, 1000, 367);
--------------------------------------------------------------------------------------------------------------------------------------

+ SELECT * FROM books;


+ SELECT title from books;
--------------------------------------------------------------------------------------------------------------------------------------

# concatination

+ SELECT CONCAT(author_fname, ' ', author_lname) FROM books;


+ SELECT CONCAT(author_fname, ' ', author_lname) AS 'full name' FROM books;
+ SELECT author_fname AS first, author_lname AS last, CONCAT(author_fname, ' ', author_lname)
AS full FROM books;
+ SELECT author_fname AS first, author_lname AS last, CONCAT(author_fname, ', ', author_lname)
AS full FROM books;
+ SELECT CONCAT(title, '-', author_fname, '-', author_lname) FROM books;
+ SELECT CONCAT_WS(' - ', title, author_fname, author_lname) FROM books;
--------------------------------------------------------------------------------------------------------------------------------------
# substring
+ SELECT substring( title, 1,10) FROM books;
+ SELECT substring( title, 7) as 'Small Title' FROM books;
+ SELECT substring( title, -8) FROM books;
# both concatation and substring operation
+ SELECT CONCAT(SUBSTRING(title, 1, 10), '....') as 'Short Title' FROM books;
-- here substring operation is done 1st later concation –
--------------------------------------------------------------------------------------------------------------------------------------
# replacing
+ SELECT REPLACE( title, 'The', '***') as 'Replaced word' FROM books;
--------------------------------------------------------------------------------------------------------------------------------------
# both Replacing and substring operation
+ SELECT SUBSTRING( REPLACE( title, 'e', '6') , 1, 10) as 'Replaced word' FROM books;
--------------------------------------------------------------------------------------------------------------------------------------
# group by
+ SELECT author_lname, COUNT(book_id) as no_of_author_having_samename
from books GROUP BY author_lname;
--------------------------------------------------------------------------------------------------------------------------------------
# having
+ SELECT author_lname, COUNT(book_id) as no_of_author_having_samename
from books GROUP BY author_lname HAVING COUNT(book_id)>=3;
--------------------------------------------------------------------------------------------------------------------------------------
#upper and lower
+ SELECT upper(title) AS TITLE, CONCAT(author_fname, ' ', author_lname), released_year FROM
BOOKS ;
+ SELECT LOWER(title), CONCAT(author_fname, ' ', author_lname), released_year FROM BOOKS ;
+ SELECT Lcase(title) FROM BOOKS ;
--------------------------------------------------------------------------------------------------------------------------------------
# To calculate length of a character
+ SELECT character_length(title) from books;
+ SELECT char_length(title) from books;
+ SELECT author_lname, char_length(author_lname) AS 'no of letters' FROM BOOKS;
+ SELECT CONCAT( title,' ', 'is', ' ', char_length(title), ' ', 'Character long') from books;
+ SELECT CONCAT( title,' is ', char_length(title), ' Character long') from books;
--------------------------------------------------------------------------------------------------------------------------------------
# Reverse
+ SELECT reverse(title) FROM BOOKS ;
+ SELECT CONCAT( 'WOOF', ' ', REVERSE('WOOF')); -- EXAMPLE --
+ SELECT CONCAT( author_fname, ' ', REVERSE(author_fname)) FROM BOOKS;
--------------------------------------------------------------------------------------------------------------------------------------
# DISTINCT
+ SELECT DISTINCT author_fname, author_lname from books;
+ SELECT DISTINCT CONCAT( author_fname,' ', author_lname) from books;
+ SELECT DISTINCT author_fname, author_lname
FROM books
ORDER BY author_fname, author_lname;
--------------------------------------------------------------------------------------------------------------------------------------
# order by
+ SELECT released_year from books ORDER BY released_year ;
+ SELECT author_lname from books ORDER BY author_lname;
+ SELECT author_lname from books ORDER BY author_lname ASC;
+ SELECT author_lname from books ORDER BY author_lname DESC;
+ SELECT TITLE, released_year, PAGES from books ORDER BY released_year ;
+ SELECT TITLE, PAGES from books ORDER BY released_year ;
+ SELECT DISTINCT TITLE, released_year, PAGES from books ORDER BY released_year ;
+ SELECT DISTINCT author_lname from books ORDER BY author_lname ASC;
EXAMPLE
SELECT title, author_fname, author_lname from books ORDER BY 2;
# What does 2 efers to ?......
# well instesd of typing the column name again we can just type '2' and that refers to
"author_fname",
# similarly we can type 3 for author_lname & 1 for title

SELECT title, author_fname, author_lname from books ORDER BY author_lname, author_fname ;


# 1st author_lname and then author_fname
SELECT CONCAT(author_fname, ' ', author_lname) as name FROM books ORDER BY author_fname ;
SELECT CONCAT(author_fname, ' ', author_lname) AS name FROM books ORDER BY author_fname
ASC;
SELECT CONCAT(author_fname, ' ', author_lname) AS name FROM books ORDER BY author_fname
DESC ;
--------------------------------------------------------------------------------------------------------------------------------------
# LIMIT
SELECT title from books LIMIT 4;
SELECT * from books LIMIT 1 ;
SELECT * from books ORDER BY titlE LIMIT 7;
SELECT substring(title, 3,1) as '1 letter' from books;
SELECT * from books LIMIT 0,1;
SELECT * from books LIMIT 4,1; # Prints 5th row here 4 will be initial point and 1 will be like how
many row it shd print (remember substring)
SELECT * from books ORDER BY released_year DESC LIMIT 0, 5;
SELECT * FROM books order by book_id desc, author_fname asc limit 0,7;
SELECT * FROM books order by pages desc, author_fname asc limit 0,7;
--------------------------------------------------------------------------------------------------------------------------------------
# LIKE
# TYPE 1
SELECT author_fname from books WHERE author_fname LIKE "%da%";
SELECT author_fname from books WHERE author_fname LIKE "da%";
SELECT author_fname from books WHERE author_fname LIKE "%da";
SELECT * from books WHERE TITLE LIKE "the"; # no output coz we don't have only 'the or THE '
SELECT * from books WHERE TITLE LIKE "the%";
SELECT * from books WHERE TITLE LIKE "%the";
SELECT * from books WHERE TITLE LIKE "%the%";
# TYPE 2
+ SELECT stock_quantity from books WHERE stock_quantity LIKE '___'; (3 underscore i.e 3digit num)
+ SELECT DISTINCT author_fname from books WHERE author_fname LIKE '____'; (4-underscrore)

# Iam searching for the book with % in it


# Iam serarching for the book name with _(underscore) in it , what to do
# so in this case we use backward slash (\) for example
SELECT * from books WHERE TITLE LIKE "%\%%";
SELECT * from books WHERE TITLE LIKE "%\_%";
--------------------------------------------------------------------------------------------------------------------------------------
# numeric function
select abs(-3);
select mod(10, 4) as remainder; # here i have given a aliase name as reminder

select power(4, 2) as square;


select power(10, 3) as cubes;

select sqrt(144) as 'square root';

select greatest(2, 5, 18, 6, 12);


SELECT LEAST(2, 5, 18, 6, 12);

select truncate(22.3786, 1); # deciding how many decimal point to be printed


select truncate(22.3786, 2);

select round(22.879);
select round(22.43454);
select round(22.879, 2); # as we can notice here 22.879 as turnned to 22.88 because we have
specified the num for how many digit it shd be rounded off
--------------------------------------------------------------------------------------------------------------------------------------
# PRACTICE

# v7
+ SELECT upper(REVERSE(title)) from books;
+ SELECT REPLACE (CONCAT('I', ' ', 'LIKE', ' ', 'PESCE'), ' ', '_');
+ SELECT REPLACE( title, ' ', '->') from books;
+ select author_lname as forward, reverse(author_lname) as Backword from books;
+ SELECT UPPER(concat(author_fname, ' ', author_lname)) 'FULL NAME' from books;
+ SELECT CONCAT(title, " was released in the year ", released_year ) as blurb FROM books;
+ SELECT title, char_length(title) as 'character_length' from books ;
+ SELECT CONCAT(SUBSTRING(title, 1, 10), '.....') AS 'SHORT TITLE',
CONCAT(author_lname, ', ', author_fname) AS 'AUTHOR',
CONCAT(stock_quantity, ' in stock') AS 'QUANTITY' FROM BOOKS;

# v8
+ SELECT TITLE, PAGES FROM BOOKS ORDER BY PAGES DESC LIMIT 1;
+ SELECT concat(title, released_year) as summary from books order by released_year desc limit 0 , 3;
+ SELECT TITLE, author_lname FROM BOOKS WHERE author_lname LIKE '% %';
+ SELECT * from books order by stock_quantitY Asc, TITLE ASC limit 0 , 3;
+ select title, author_lname from books order by author_lname asc;
+ select concat('MY FAV AUTHOR is ',UPPER (author_fname),' ', UPPER(author_lname) ) from books
order by author_lname asc;
+ SELECT title, author_lname FROM books ORDER BY 2,1;

You might also like