You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SELECT*FROM users;
SELECT first_name, last_name FROM users;
Where Clause
SELECT*FROM users WHERE location='Massachusetts';
SELECT*FROM users WHERE location='Massachusetts'AND dept='sales';
SELECT*FROM users WHERE is_admin =1;
SELECT*FROM users WHERE is_admin >0;
SELECT*FROM users ORDER BY last_name ASC;
SELECT*FROM users ORDER BY last_name DESC;
Concatenate Columns
SELECT CONCAT(first_name, '', last_name) AS'Name', dept FROM users;
Select Distinct Rows
SELECT DISTINCT location FROM users;
Between (Select Range)
SELECT*FROM users WHERE age BETWEEN 20AND25;
Like (Searching)
SELECT*FROM users WHERE dept LIKE'd%';
SELECT*FROM users WHERE dept LIKE'dev%';
SELECT*FROM users WHERE dept LIKE'%t';
SELECT*FROM users WHERE dept LIKE'%e%';
Not Like
SELECT*FROM users WHERE dept NOT LIKE'd%';
IN
SELECT*FROM users WHERE dept IN ('design', 'sales');
Create & Remove Index
CREATEINDEXLIndexOn users(location);
DROPINDEX LIndex ON users;
New Table With Foreign Key (Posts)
CREATETABLEposts(
id INT AUTO_INCREMENT,
user_id INT,
title VARCHAR(100),
body TEXT,
publish_date DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(id),
FOREIGN KEY (user_id) REFERENCES users(id)
);
Add Data to Posts Table
INSERT INTO posts(user_id, title, body) VALUES (1, 'Post One', 'This is post one'),(3, 'Post Two', 'This is post two'),(1, 'Post Three', 'This is post three'),(2, 'Post Four', 'This is post four'),(5, 'Post Five', 'This is post five'),(4, 'Post Six', 'This is post six'),(2, 'Post Seven', 'This is post seven'),(1, 'Post Eight', 'This is post eight'),(3, 'Post Nine', 'This is post none'),(4, 'Post Ten', 'This is post ten');
SELECTCOUNT(id) FROM users;
SELECTMAX(age) FROM users;
SELECTMIN(age) FROM users;
SELECTSUM(age) FROM users;
SELECT UCASE(first_name), LCASE(last_name) FROM users;
Group By
SELECT age, COUNT(age) FROM users GROUP BY age;
SELECT age, COUNT(age) FROM users WHERE age >20GROUP BY age;
SELECT age, COUNT(age) FROM users GROUP BY age HAVINGcount(age) >=2;