500 likes | 512 Views
SQL LANGUAGE TUTORIAL. Prof: Dr. Shu-Ching Chen TA: Haiman Tian. Basic Syntax of SQL Language. SELECT attribute name(s) FROM table name WHERE comparison predicate (Boolean expression) GROUP BY attribute name (s) HAVING comparison predicate ORDER BY attribute name (s). Create Tables.
E N D
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Basic Syntax of SQL Language SELECT attribute name(s) FROM table name WHERE comparison predicate (Boolean expression) GROUP BY attribute name(s) HAVING comparison predicate ORDER BY attribute name(s)
SQL ALTER TABLE ADD COLUMN ALTER TABLE table name ADD column_name datatype;
Formula • Like & DISTINCT
SELECT ALL with LIKE operator SELECT column1, column2, … FROM table_name WHERE columnN LIKE pattern;
SELECT ALL with LIKE operator Example: WHERE column_name LIKE 'a%'
SELECT ALL with LIKE operator Example: WHERE column_name LIKE ’%a'
SELECT IN You use the IN operator in the WHERE clause to check if a value matches any value in a list of values. The syntax of the IN operator is as follows: SELECT column1, column2, … FROM table_name WHERE columnN IN (value1, value2, …); The expression returns true if the value matches any value in the list i.e., value1, value2, etc. The list of values is not limited to a list of numbers or strings but also a result set of a SELECT statement as shown in the following query: SELECT column1, column2, … FROM table_name WHERE columnN IN (SELECT value FROM tbl_name);
SELECT NOTIN You can combine the IN operator with the NOT operator to select rows whose values do not match the values in the list. SELECT column1, column2, … FROM table_name WHERE columnN NOTIN (value1, value2, …);
SELECT ANY (1) • The ANY operator must be preceded by one of the following operator =,<=,>,<,>,<> • The ANY operator returns true if any value of the subquery meets the condition, otherwise, it returns false. • The subquery must return exactly one column. SELECT column1, column2, … FROM table_name WHERE columnN operator ANY (subquery);
SELECT ANY (2) • The = ANY is equivalent to IN operator. • Note that the <>ANY operator is different from NOT IN. • x<>ANY(a,b,c) is equivalent to • x<>a OR x<>b OR x<>c
SELECT from two TABLES (1) Products Categories
SELECT from two TABLES (2) • Query: List the name(s) of all products that are categorized as beverages.
ASCII Value (2) • Example select grade, ascii(grade) from enroll order by ascii(grade)
SQL IN OPERATOR AVG() - Returns the average value COUNT() - Returns the number of rows FIRST() - Returns the first value LAST() - Returns the last value MAX() - Returns the largest value MIN() - Returns the smallest value SUM() - Returns the sum
SQL Joins • INNER JOIN: Return rows when there is at least one match in both tables • LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table • RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table SELECT column_name(s) FROM table_name1 JOIN_TYPES table_name2 ON table_name1.column_name = table_name2.column_name
SQL UPDATE FIELD UPDATE table name SET column1 = value, column2 = value2,… WHERE comparison predicate (Boolean expression)
SQL UPDATE Join (1) Sometimes, you need to update data of a table based on values in another table. In this case, you can use the PostgreSQL UPDATE join syntax as follows: UPDATE A SET A.c1 = expression FROM B WHERE A.c2 = B.c2
SQL UPDATE Join (2) product_segment product • The product_segment table has the discount column that stores the discount percentage based on a specific segment. For example, grand luxury products have 5% discount while luxury and mass products have 6% and 10% discount respectively. • The product table has the foreign key column segment_id that links to the id of the segment table.
SQL UPDATE Join (3) product_segment product • The product_segment table has the discount column that stores the discount percentage based on a specific segment. For example, grand luxury products have 5% discount while luxury and mass products have 6% and 10% discount respectively. • The product table has the foreign key column segment_id that links to the id of the segment table.
SQL UPDATE Join (4) UPDATE product SETnet_price = price – price * discount FROM product_segment WHERE product.segment_id = product_segment.id; This statement joins the product table to the product_segment table. If there is a match in both tables, it gets the discount from the product_segment table, calculates the net price based on the following formula, and updates the net_pricecolumn. net_price = price – price * discount;
SELECT GROUP BY (1) Products Categories
SELECT GROUP BY (2) • Query: Count the number of products in each category.
SELECT INNER JOIN • Query: Count the number of products in each category (using inner join).
SELECT GROUP BY (3) • Query: List the category names that contain greater-than-or-equal to 3 products (nesting the previous query)
SELECT HAVING (1) Products Categories
SELECT HAVING (2) • Query: List the category names that contain greater-than-or-equal to 3 products (without nesting query)
SQL DATE Type (3) • date_trunc: function truncates a TIMESTAMP value based on a specified date part (e.g. hour, week, month) date_trunc(‘datepart’, field)
SQL DATE Type (4) • date() function converts a string literal to a date value. The following is the syntax: date(‘DD/MM/YYYY’)