We could list individual column names separated by commas, but we use the wildcard *, which means "all the columns."
Where clause
The WHERE clause is a powerful filtering tool that can be used with equality operators like less than (<) or not equal to (!=). We can also use the OR and AND logical operators to evaluate multiple conditions. If the expression evaluates to true, the row is returned.
SELECT first_name, project_id
FROM customers
WHERE project_id =1;
Schema
instead of setting up the db via cli, we make a file to setup for the database and tables
seed.sql to conatain out data to instrt into DATABASE
db.sql to have information on database and creation of
schema.sql to contain database table information and creation.
Deleting and updating columns
DELETEFROM custmers,
WHERE first_name = "Montague";
UPDATE customers
SET project_id =1WHERE id =3;
SQL Data Types
VARCHAR: variable character representing a string of characters with a maximum length of 255 characters.
BOOLEAN: a boolean value that can be either true or false.
TEXT: a variable character string with a maximum length of 65,535 characters.
INTEGER: a number that is stored without a decimal point.
DATE: a date and time value.
DATETIME: a date and time value.
TIMESTAMP: a date and time value.
REAL: a number that is stored with a decimal point.
FLOAT: a number that is stored with a decimal point.
Building relationships
CONSTRAINT: a constraint is a rule that must be satisfied before a database operation can be performed.
FOREIGN KEY: a reference to another table, using the REFERENCES clause, here we reference the primary key of the other table.
connection.query('UPDATE customers SET project_id = ? WHERE id = ?', [1, 3], function(err, results, fields) {
if (err) throw err;
console.log(results);
});
the ? is a placeholder for the value we want to insert into the query.
[1, 3] is the value we want to insert into the query when
Deleting data from db
connection.query('DELETE FROM customers WHERE id = ?', [3], function(err, results, fields) {
if (err) throw err;
console.log(results);
});
query call back function objects
A query callback function is a function that is called when a query is completed. it returns with it
err: an error object if the query failed.
results: an array of rows returned by the query. some properties in results
affectedRows: the number of rows affected by the query.
insertId: the id of the last inserted row.
warningStatus: the warning status returned by the server.
fields: an array of metadata about the columns in the result set.
additional fun sql commands
-- get all voters who do not have a last name of Cooper or Jarman SELECT * FROM voters WHERE last_name != 'Cooper' AND last_name != 'Jarman';
-- get all voters who have a .qUni email address SELECT * FROM voters WHERE email LIKE '%.qUni';
-- get only the last created voter SELECT * FROM voters ORDER BY created_at DESC LIMIT 1;
-- get all voters who have a .qUni email address and have a last name of Cooper or Jarman SELECT * FROM voters WHERE email LIKE '%.qUni' AND (last_name = 'Cooper' OR last_name = 'Jarman');
-- get all voters who have a .qUni email address and have a last name of Cooper or Jarman and have a first name of John SELECT * FROM voters WHERE email LIKE '%.qUni' AND (last_name = 'Cooper' OR last_name = 'Jarman') AND first_name = 'John';
-- get all voters who have a .qUni email address and have a last name of Cooper or Jarman and have a first name of John and have a phone number of 123-456-7890 SELECT * FROM voters WHERE email LIKE '%.qUni' AND (last_name = 'Cooper' OR last_name = 'Jarman') AND first_name = 'John' AND phone_number = '123-456-7890';
Other useful aggregate functions in SQL include:
AVG() to return the average value within a group
COUNT() to return the number of rows within a group
SUM() to add up all of the values in a group
MIN() to return the smallest value within a group
MIN() to return the minimum value of a group
SELECTAVG(age) FROM voters;
OUTPUT FILTERS
GROUP BY: used to group the results of a query by a column.
HAVING: used to filter the results of a query by a condition.
ORDER BY: used to order the results of a query by a column.
LIMIT: used to limit the number of results returned by a query.
SELECTCOUNT(age) FROM voters GROUPBY age HAVING age <100;