You are on page 1of 5

SELECT ... FROM table_a JOIN table_b ...

SELECT ... FROM table_a, table_b ...

SELECT Loc FROM department, employee WHERE employee.name = 'Marston A' AND employee.deptno = department.deptno;

INSERT INTO table_1 SELECT column1, column2 FROM table_2; INSERT INTO table_1 VALUES (NULL, 8);

INSERT INTO newsales (100, '11/01/2007', 10); INSERT INTO employee (name, empno, deptno, dob, sex, edlev) VALUES ('SMITH T', 10021, 700, 460729, 'F', 16); INSERT INTO promotion (deptnum, empname, yearsexp) SELECT deptno, name, yrsexp FROM employee WHERE yrsexp > 10 ; INSERT INTO promotion SELECT name, deptno, yrsexp FROM employee WHERE yrsexp > 10 ; INSERT INTO employee (name, empno, deptno, dob, sex, edlev) VALUES ('Orebo B', 10005, 300, 'Nov 17 1957', 'M', 18) ; INSERT INTO employee (10005,'Orebo B',300,,,,'Nov 17 1957', 'M',,,18,); INSERT INTO employee (10005,'Orebo B',300,,,,'Nov 17 1957', 'M',,,18,); INSERT INTO backorders (productid, quantity, unitprice) SELECT AND CONSUME TOP 1 productid, quantity, unitprice FROM orders_qt; INSERT INTO table7 VALUES (1, 2, DEFAULT(col2), 'aaa'); INSERT INTO table7 (col1, col3, col4) VALUES (1, DEFAULT(col2), 'bbb');

INSERT INTO table7 VALUES (1, 2, DEFAULT (col2)+5, 'aaa'); INSERT INTO table7 (col1, col3, col4) VALUES (1, DEFAULT(col2)+5, 'bbb'); INSERT INTO t SELECT * FROM s LOGGING ERRORS WITH NO LIMIT; INSERT INTO t SELECT * FROM s LOGGING ERRORS WITH LIMIT OF 100; INSERT INTO tab1 VALUES (0, 'POINT(10 20)'); INSERT INTO t1 VALUES (1,2,3 (SELECT d2 FROM t2 WHERE a2=t1.a1)); INSERT INTO t1 VALUES (1, PERIOD(DATE '2005-02-03', DATE '2006-02-04')); INSERT INTO t1 SELECT a, PERIOD(b, c) FROM t2; INSERT INTO t1 VALUES (1, test.value_expression(2, 3), 4);

select case when(cast((LEC_STRT_DATE(format 'YYYY-MM-DD')) as char(10)) is null) then '' else cast((LEC_STRT_DATE(format 'YYYY-MM-DD')) as char(10)) end from CUST_PROF_FCT sample 10

Select SELECT name FROM personnel p WHERE salary = (SELECT MAX(salary) FROM personnel sp WHERE p.department=sp.department); SELECT pubname FROM publisher WHERE 0 = (SELECT COUNT(*) FROM book WHERE book.pubnum=publisher.pubnum);

Arithmetic expressions
SELECT (fix_cost + (SELECT SUM(part_cost) FROM parts)) AS total_cost,

String expressions
SELECT (SELECT prod_name FROM prod_table AS p WHERE p.pno = s.pno) || store_no

CASE expressions
SELECT CASE WHEN (SELECT count(*) FROM inventory WHERE inventory.pno = orders.pno) > 0 THEN 1 ELSE 0 END,

Aggregate expressions
SELECT SUM(SELECT count(*) FROM sales WHERE sales.txn_no = receipts.txn_no),

Value list expressions


WHERE txn_no IN (1,2, (SELECT MAX(txn_no) FROM sales WHERE sale_date = CURRENT_DATE)); SELECT * FROM orders, lineitem WHERE orders.PARTITION = 3 AND lineitem.PARTITION = 5 AND orders.o_orderkey = Lineitem.l_orderkey; SELECT orders.*, lineitem.*, orders.PARTITION FROM orders, lineitem WHERE orders.PARTITION = 3 AND lineitem.PARTITION = 5 AND orders.o_orderkey = lineitem.l_orderkey;

This example is not valid because PARTITION cannot be referenced in the VALUES clause:
INSERT INTO Orders VALUES (PARTITION, 10, 'A', 599.99, DATE '2001-02-07','HIGH', 'Jack', 3,'Invalid insert'); SELECT DISTINCT PARTITION (FORMAT '999') FROM Orders ORDER BY PARTITION; SELECT DISTINCT CAST (PARTITION AS BYTEINT) FROM Orders ORDER BY PARTITION;

The following two examples provide a list of the populated partitions in the orders table:
SELECT DISTINCT PARTITION (FORMAT '999') FROM Orders ORDER BY PARTITION; SELECT DISTINCT CAST (PARTITION AS BYTEINT) FROM Orders ORDER BY PARTITION;

The following example counts the number of rows in each populated partition:
SELECT PARTITION, COUNT(*) FROM Orders GROUP BY PARTITION ORDER BY PARTITION;

So you now use an EXPAND ON clause to retrieve the employee details specifying an interval constant period.
SELECT emp_id, emp_name, job_duration AS tsp FROM employee EXPAND ON job_duration AS tsp BY INTERVAL '1' YEAR FOR PERIOD(DATE '2006-01-01', DATE '2008-01-01'); SELECT AND CONSUME TOP 1 * FROM shopping_cart; SELECT product_id, quantity FROM (SELECT stocked.product_id, stocked.quantity FROM stocked, product WHERE stocked.product_id = product.product_id AND product.on_hand > 5) AS orderable_items WHERE quantity < 10; SELECT TOP 10 * FROM orders;

The QUALIFY clause with the RANK or ROW_NUMBER ordered analytical functions returns the same results as the TOP n operator. For example, the following two requests have the same semantics:
SELECT TOP 10 * FROM sales ORDER BY county; SELECT * FROM sales QUALIFY ROW_NUMBER() OVER (ORDER BY COUNTY) <= 10;

Similarly, the following two requests have the same semantics:


SELECT TOP 10 WITH TIES * FROM sales ORDER BY county; SELECT * FROM sales QUALIFY RANK() OVER (ORDER BY county) <= 10;

Aggregates are not allowed in a WHERE clause predicate; however, derived tables make such calculations easy to perform. The following example shows how derived tables facilitate this.
SELECT name, salary, average_salary FROM (SELECT AVG(salary) FROM employee) AS workers (average_salary), employee WHERE salary > average_salary ORDER BY salary DESC; DELETE FROM orders WHERE orders.PARTITION = 1; SELECT name, loc FROM employee, department WHERE employee.dept_no = department.dept_no AND employee.dept_no IN (SELECT dept_no FROM department WHERE mgr_no = 10007); SELECT emp_no, name, job_title, salary, yrs_exp FROM employee WHERE (salary,yrs_exp) >= ALL (SELECT salary,yrs_exp FROM employee);

SELECT item, COUNT(store) FROM (SELECT store,item,profit,QUANTILE(100,profit) AS percentile FROM (SELECT it.item, SUM(sales)COUNT(sales) * it.item_cost) AS profit FROM daily_sales AS ds, items AS it WHERE ds.item = it.item GROUP BY it.item,it.item_cost) AS item_profit GROUP BY store, it, profit QUALIFY percentile = 0) AS top_one_percent GROUP BY item HAVING COUNT(store) >= 20;

SELECT * FROM order_table AS o_1 WHERE order_amount < (SELECT MAX (order_amount) FROM order_table AS o_2 WHERE o_1 . status = o_2.status) ;
SELECT dept_no, SUM(salary) FROM employee GROUP BY dept_no;
1. Load the data into a multiset table. If the target table must be a set table, you can "insert ... select" into the target table from the multiset table, and duplicate rows will be suppressed automatically. 2. By default, character columns in Teradata have the NOT CASESPECIFIC attribute, which causes character values that differ only in case to be considered equal. You may need to explicitly specify CASESPECIFIC if the default is not suitable for your application.

However, when using an UPDATE and the data value of a PI is changed, there is more processing required than when changing the content of any other column. This is due to the fact that the original row must be read, literally deleted from the current AMP and 1. rehashed, redistributed and inserted on another AMP based on the new data value.

You might also like