SQL is the standard language for dealing with Relational Databases. SQL is used to insert, search, update, and delete database records.
Reactjs
Features of React.js: There are unique features are available on React because that it is widely popular.
Use sql: It is faster than normal JavaScript as it performs optimizations while translating to regular JavaScript. It makes it easier for us to create templates.
Virtual DOM: Virtual DOM exists which is like a lightweight copy of the actual DOM. So for every object that exists in the original DOM, there is an object for that in React Virtual DOM. It is exactly the same, but it does not have the power to directly
change the layout of the document. Manipulating DOM is slow, but manipulating Virtual DOM is fast as nothing gets drawn on the screen.
One-way Data Binding: This feature gives you better control over your application.
Component: A Component is one of the core building blocks of React. In other words, we can say that every application you will develop in React will be made up of pieces called components. Components make the task of building UIs much easier. You can
see a UI broken down into multiple individual pieces called components and work on them independently and merge them all in a parent component which will be your final UI.
Performance: React.js use sql, which is faster compared to normal JavaScript and HTML. Virtual DOM is a less time taking procedure to update webpages content.
SELECT Statement
SELECT Statement
The SELECT statement is used to select data from a database.
The data returned is stored in a result table, called the result-set.
SELECT column1, column2, ...
FROM table_name;
select all the fields available in the table
SELECT * FROM table_name;
SELECT DISTINCT Statement
SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct (different) values.
Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values
SELECT DISTINCT column1, column2, ...
FROM table_name;
For counts of unique entry
SELECT COUNT(DISTINCT Country) FROM Custome
WHERE Clause
WHERE Clause
The WHERE clause is used to filter records.
It is used to extract only those records that fulfill a specified condition.
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Operators in The WHERE Clause
Operators in The WHERE Clause
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
AND, OR and NOT Operators
AND, OR and NOT Operators
TThe WHERE clause can be combined with AND, OR, and NOT operators.
The AND and OR operators are used to filter records based on more than one condition:
The AND operator displays a record if all the conditions separated by AND are TRUE.
The OR operator displays a record if any of the conditions separated by OR is TRUE.
The NOT operator displays a record if the condition(s) is NOT TRUE.
AND Operator
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR Operator
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
NOT Operator
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
ORDER BY Keyword
ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
INSERT INTO Statement
INSERT INTO Statement
INSERT INTO statement is used to insert new records in a table.
It is possible to write the INSERT INTO statement in two ways:
1. Specify both the column names and the values to be inserted:
2. If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table.
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
NULL Value
Null value
A field with a NULL value is a field with no value.
If a field in a table is optional, it is possible to insert a new record or update a record without adding a value to this field. Then, the field will be saved with a NULL value.
Note: 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!
How to Test for NULL Values? 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.
IS NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
IS NOT NULL Syntax/h3>
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
UPDATE Statement
UPDATE Statement
The UPDATE statement is used to modify the existing records in a table. Notice the WHERE clause in the UPDATE statement. The WHERE clause specifies which record(s) that should be updated. If you omit the WHERE clause, all
records in the table will be updated!
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
DELETE Statement
DELETE Statement
The DELETE statement is used to delete existing records in a table. The WHERE clause specifies which record(s) should be deleted. If you omit the WHERE clause, all records in the table will be deleted!
DELETE FROM table_name WHERE condition;
Delete all rows in a table without deleting the table
DELETE FROM table_name;
LIMIT Clause
LIMIT Clause
The LIMIT clause is used to specify the number of records to return.
The LIMIT clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
MIN() and MAX() Functions
MIN() and MAX() Functions
The LIMIT clause is used to specify the number of records to return.
The LIMIT clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.
MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;
SELECT MIN(Price) AS SmallestPrice
FROM Products;
MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;
SELECT MAX(Price) AS LargestPrice
FROM Products;
COUNT()
COUNT()
The COUNT() function returns the number of rows that matches a specified criterion.
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
AVG()
AVG()
The AVG() function returns the average value of a numeric column.
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SUM()
SUM()
The SUM() function returns the total sum of a numeric column.
SELECT SUM(column_name)
FROM table_name
WHERE condition;
LIKE Operator
LIKE Operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator:
The percent sign (%) represents zero, one, or multiple characters
The underscore sign (_) represents one, single character
The percent sign and the underscore can also be used in combinations!
Tip: You can also combine any number of conditions using AND or OR operators.
LIKE Operator
Description
WHERE CustomerName LIKE 'a%'
Finds any values that start with "a"
WHERE CustomerName LIKE '%a'
Finds any values that end with "a"
WHERE CustomerName LIKE '%or%'
Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%'
Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%'
Finds any values that start with "a" and are at least 2 characters in length
WHERE CustomerName LIKE 'a__%'
Finds any values that start with "a" and are at least 3 characters in length
WHERE ContactName LIKE 'a%o'
Finds any values that start with "a" and ends with "o"
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';
Wildcard Characters
Wildcard Characters
A wildcard character is used to substitute one or more characters in a string.
Wildcard characters are used with the LIKE operator. The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
Symbol
Description
Example
%
Represents zero or more characters
bl% finds bl, black, blue, and blob
_
Represents a single character
h_t finds hot, hat, and hit
SELECT * FROM Customers
WHERE City LIKE '%es%';
SELECT * FROM Customers
WHERE City LIKE 'L_n_on';
IN Operator
IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.
The IN operator is a shorthand for multiple OR conditions.
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
BETWEEN Operator
BETWEEN Operator
The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.
The BETWEEN operator is inclusive: begin and end values are included.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Aliases
Aliases
Aliases are used to give a table, or a column in a table, a temporary name.
Aliases are often used to make column names more readable.
An alias only exists for the duration of that query.
An alias is created with the AS keyword.
Note: Single or double quotation marks are required if the alias name contains spaces
SELECT CustomerName AS Customer,
ContactName AS "Contact Person"
FROM Customers;
Alias Column Syntax
SELECT column_name AS alias_name
FROM table_name;
Alias Table Syntax
SELECT column_name(s)
FROM table_name AS alias_name;
Joining Tables
Joining Tables
A JOIN clause is used to combine rows from two or more tables, based on a related column between them.
Supported Types of Joins in MySQL:
INNER JOIN: Returns records that have matching values in both tables
LEFT JOIN: Returns all records from the left table, and the matched records from the right table
RIGHT JOIN: Returns all records from the right table, and the matched records from the left table
CROSS JOIN: Returns all records from both tables
Note: CROSS JOIN can potentially return very large result-sets!
Self Join: A self join is a regular join, but the table is joined with itself. Note:T1 and T2 are different table aliases for the same table.
INNER JOIN Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
LEFT JOIN Syntax
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
RIGHT JOIN Syntax
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
CROSS JOIN Syntax
SELECT column_name(s)
FROM table1
CROSS JOIN table2;
Self Join Syntax
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
UNION Operator
UNION Operator
The UNION operator is used to combine the result-set of two or more SELECT statements.
Every SELECT statement within UNION must have the same number of columns
The columns must also have similar data types
The columns in every SELECT statement must also be in the same order
Note: The column names in the result-set are usually equal to the column names in the first SELECT statement.
The UNION operator selects only distinct values by default. To allow duplicate values, use
UNION ALL
UNION Syntax
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
UNION ALL Syntax
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
GROUP BY Statement
GROUP BY Statement
The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country".
The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Example:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
HAVING Clause
HAVING Clause
The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Example:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
EXISTS Operator
EXISTS Operator
The EXISTS operator is used to test for the existence of any record in a subquery.
The EXISTS operator returns TRUE if the subquery returns one or more records.
EXISTS Syntax
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
Example:
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID = Suppliers.supplierID AND Price < 20);
ANY Operator
ANY Operator
The ANY operator:
returns a boolean value as a result
returns TRUE if ANY of the subquery values meet the condition
ANY means that the condition will be true if the operation is true for any of the values in the range.
Note: The operator must be a standard comparison operator (=,
<>, !=, >, >=,
<, or <=).
ANY Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name
FROM table_name
WHERE condition);
Example:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
ALL Operator
ALL Operator
The ALL operator:
returns a boolean value as a result
returns TRUE if ALL of the subquery values meet the condition
is used with SELECT, WHERE and HAVING statements
ALL means that the condition will be true only if the operation is true for all values in the range.
Note: The operator must be a standard comparison operator (=,
<>, !=, >, >=,
<, or <=).
ALL Syntax With SELECT
SELECT ALL column_name(s)
FROM table_name
WHERE condition;
ALL Syntax With WHERE or HAVING
SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL
(SELECT column_name
FROM table_name
WHERE condition);
Example:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
INSERT INTO SELECT Statement
INSERT INTO SELECT Statement
The INSERT INTO SELECT statement copies data from one table and inserts it into another table.
The INSERT INTO SELECT statement requires that the data types in source and target tables matches.
Note: The existing records in the target table are unaffected.
INSERT INTO SELECT Syntax(Copy all columns from one table to another table)
INSERT INTO table2
SELECT * FROM table1
WHERE condition;
Copy only some columns from one table into another table
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;
CASE Statement
CASE Statement
The CASE statement goes through conditions and returns a value when the first condition is met (like an if-then-else statement). So, once a condition is true, it will stop reading and return the result. If no conditions are true, it returns the value
in the ELSE clause.
If there is no ELSE part and no conditions are true, it returns NULL.
CASE Syntax
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
Example:
SELECT OrderID, Quantity,
CASE
WHEN Quantity > 30 THEN 'The quantity is greater than 30'
WHEN Quantity = 30 THEN 'The quantity is 30'
ELSE 'The quantity is under 30'
END AS QuantityText
FROM OrderDetails;
IFNULL()
CASE Statement
The MySQL IFNULL() function lets you return an alternative value if an expression is NULL.
CASE Syntax
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
Example:
SELECT OrderID, Quantity,
CASE
WHEN Quantity > 30 THEN 'The quantity is greater than 30'
WHEN Quantity = 30 THEN 'The quantity is 30'
ELSE 'The quantity is under 30'
END AS QuantityText
FROM OrderDetails;
COALESCE() Function
CASE
SELECT ProductName, UnitPrice * (UnitsInStock + COALESCE(UnitsOnOrder, 0))
FROM Products;
MySQL Comments
MySQL Comments
Comments are used to explain sections of SQL statements, or to prevent execution of SQL statements.
Single Line Comments:Single line comments start with --.
Any text between -- and the end of the line will be ignored (will not be executed).
Multi-line Comments: Multi-line comments start with /* and end with */.
Any text between /* and */ will be ignored.
Single Line Comments
-- Select all:
Multi-line Comments
/*Select all the columns
of all the records
in the Customers table:*/
MySQL Operators
Arithmetic Operators
Arithmetic Operators
Operator
Description
+
Add
-
Subtract
*
Multiply
/
Divide
%
Modulo
Bitwise Operators
Bitwise Operators
Operator
Description
&
Bitwise AND
|
Bitwise OR
^
Bitwise exclusive OR
Comparison Operators
Comparison Operators
Operator
Description
=
Equal to
>
Greater than
<
Less than
>=
Greater than or equal to
<=
Less than or equal to
<>
Not equal to
Compound Operators
Compound Operators
Operator
Description
+=
Add equals
-=
Subtract equals
*=
Multiply equals
/=
Divide equals
%=
Modulo equals
&=
Bitwise AND equals
^-=
Bitwise exclusive equals
|*=
Bitwise OR equals
Logical Operators
Logical Operators
Operator
Description
ALL
TRUE if all of the subquery values meet the condition
AND
TRUE if all the conditions separated by AND is TRUE
ANY
TRUE if any of the subquery values meet the condition
BETWEEN
TRUE if the operand is within the range of comparisons
EXISTS
TRUE if the subquery returns one or more records
IN
TRUE if the operand is equal to one of a list of expressions
LIKE
TRUE if the operand matches a pattern
NOT
Displays a record if the condition(s) is NOT TRUE
OR
TRUE if any of the conditions separated by OR is TRUE
SOME
TRUE if any of the subquery values meet the condition
CREATE DATABASE Statement
CREATE DATABASE Statement
The CREATE DATABASE statement is used to create a new SQL database.
CREATE DATABASE databasename;
list of databases
SHOW DATABASES;
DROP DATABASE Statement
DROP DATABASE databasename;
The DROP DATABASE statement is used to drop an existing SQL database.
Note: Be careful before dropping a database. Deleting a database will result in loss of complete information stored in the database!
DROP DATABASE databasename;
CREATE TABLE Statement
CREATE TABLE Statement
The CREATE TABLE statement is used to create a new table in a database.
A copy of an existing table can also be created using CREATE TABLE.
The new table gets the same column definitions. All columns or specific columns can be selected.
If you create a new table using an existing table, the new table will be filled with the existing values from the old table.
CREATE TABLE new_table_name AS
SELECT column1, column2,...
FROM existing_table_name
WHERE ....;
);
TRUNCATE TABLE
TRUNCATE TABLE
The DROP TABLE statement is used to drop an existing table in a database.
TRUNCATE TABLE table_name;
ALTER TABLE - ADD
ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE - DROP
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE - MODIFY
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
MySQL Constraints
MySQL Constraints
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the table. If there is any violation between the constraint and the data action, the
action is aborted.
Constraints can be column level or table level. Column level constraints apply to a column, and table level constraints apply to the whole table.
The following constraints are commonly used in SQL:
NOT NULL - Ensures that a column cannot have a NULL value
UNIQUE - Ensures that all values in a column are different
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
FOREIGN KEY - Prevents actions that would destroy links between tables
CHECK - Ensures that the values in a column satisfies a specific condition
DEFAULT - Sets a default value for a column if no value is specified
CREATE INDEX - Used to create and retrieve data from the database very quickly
NOT NULL Constraint
NOT NULL Constraint
By default, a column can hold NULL values.
The NOT NULL constraint enforces a column to NOT accept NULL values.
This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
UNIQUE Constraint
UNIQUE Constraint
The UNIQUE constraint ensures that all values in a column are different.
Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint.
However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table.
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
UNIQUE (ID)
);
PRIMARY KEY Constraint
PRIMARY KEY Constraint
The PRIMARY KEY constraint uniquely identifies each record in a table.
Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
FOREIGN KEY Constraint
FOREIGN KEY Constraint
The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table.
The table with the foreign key is called the child table, and the table with the primary key is called the referenced or parent table.
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
CHECK Constraint
CHECK Constraint
The CHECK constraint is used to limit the value range that can be placed in a column.
If you define a CHECK constraint on a column it will allow only certain values for this column.
If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
DEFAULT Constraint
DEFAULT Constraint
The DEFAULT constraint is used to set a default value for a column.
The default value will be added to all new records, if no other value is specified.
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Sandnes'
);
CREATE INDEX Statement
CREATE INDEX Statement
The CREATE INDEX statement is used to create indexes in tables.
Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see the indexes, they are just used to speed up searches/queries.
Note: Updating a table with indexes takes more time than updating a table without (because the indexes also need an update). So, only create indexes on columns that will be frequently searched against.
CREATE INDEX index_name
ON table_name (column1, column2, ...);
AUTO_INCREMENT Keyword
AUTO_INCREMENT Keyword
MySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature.
By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record.
CREATE TABLE Persons (
Personid int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (Personid)
);
Date Data Types
Date Data Types
MySQL comes with the following data types for storing a date or a date/time value in the database:
DATE - format YYYY-MM-DD DATETIME - format: YYYY-MM-DD HH:MI:SS TIMESTAMP - format: YYYY-MM-DD HH:MI:SS YEAR - format YYYY or YY Note: The date data type are set for a column when you create a new
table in your database!
SELECT * FROM Orders WHERE OrderDate='2008-11-11'
CREATE VIEW Statement
CREATE VIEW Statement
In SQL, a view is a virtual table based on the result-set of an SQL statement.
A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.
You can add SQL statements and functions to a view and present the data as if the data were coming from one single table.
A view is created with the CREATE VIEW statement.
CREATE VIEW Syntax
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
CREATE VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName
FROM Customers
WHERE Country = 'Brazil';