To find out number of employee present in each department, try the following.

Select department.deptno, count (employee.employee) from employee, department where employee.deptno = department.deptno group by department.deptno

(instead, you can simply write 1 which is the column position in the SELECT statement)

Example of a sub query in Teradata SQL. The following SQL shows the Name and the Department No of the employees working for the same department under a particular manager, whose ID is 117.

select name, deptno from employee, department whereemployee.deptno = department.deptno andemployee.deptno in (select deptno from department where mgrno = 117);

If the table is very large and you want to see a sample of data, there are a couple of ways to achieve the same in Teradata. One is by using TOP operator and the other is using SAMPLE operator.

select top 10 cust_name, balance from customers order by balance desc;

Note the position of the operator changes based on which one you use. Suppose you want to EXTRACT the month from the date then use this query select extract (month from date). This following query would give 10 randomly chosen rows after showing the result set in descending order. To see the current date we use select date. Similarly for YEAR and DAY use the following commands select extract (year from date). This would give output as “11/06/2007″. How to use EXTRACT function with date and time columns Written by admin in Tuesday. November 6th 2007 under IT 4 Comments received Using Extract function with any date field is easy. This would give “11″ as the output. . balance from customers sample 10 order by balance desc. select extract (day from date). selectcust_name. Let’s try this out with system date and system time commands.The above query would give TOP 10 rows after showing the result set in descending order.

select extract (hour from time) . SELECT 2/3. To see time use the following command. / — Used to Divide. 8)). SELECT 2*3. * — Used to Multiply.66666667 but the SELECT statement has to be written as follows SELECT cast (2 as decimal (18. If you use anything else than the expected inputs for the EXTRACT function you would end up getting 3707: Syntax error. for example. August 7th 2007 under IT 9 Comments received Following are the symbols (operators) available in Teradata SQL environment that are used to calculate mathematical operations. would result in . would result in 6. This would return “15:59:19″ as the output and to extract HOUR MINUTE and SECONDS from this. Written by admin in Tuesday. SELECT 2**3. we have to use time instead.But for HOUR MINUTE and SECONDS extractions. Use the double asterisk character (**) to separate the number from the power. use the following commands select extract (minute from time). select extract (second from time) . we can not use date command. 8))/cast (3 as decimal (18. will result in 8. This is a Teradata extension to the ANSI SQL-99 standard. Arithmetic Functions and Operators in Teradata SQL. . ** — Exponentiation: Return the result of a number rose to a power (exponent). select time.

Remember that it is not the same as rounding off. without the minus sign. SELECT 2+3. SQL performs expressions enclosed in parentheses first. would give the value 3. SELECT +3. This is also a Teradata extension to the ANSI SQL-99 standard. To round off a particular decimal . would give result as 5. + — For Addition. MOD operator 7. For example. would display the value ‗-3‘. Operators of the same precedence are evaluated from left to right. Unary minus 3. The order of precedence or evaluation for arithmetic operators is as follows.– For Subtraction. SELECT ABS (-6). Multiplication 5. Addition 8. + — Also used for Unary plus (positive value). SELECT 3 mod 2 would result in 1. Exponentiation 4. would give 1. For example. Division 6. Here follows the order of precedence with numbering where 1 represents the highest and 2 as second highest so on and so forth. 1.MOD or modulo calculates the remainder in a division operation. .– Also used Unary minus (negative value). In all cases. Unary plus 2. .MOD –. SELECT -3. SELECT 3-2. The following table lists the order in which arithmetic expressions are performed when no parentheses are present. Subtraction Now let‘s look at some arithmetic functions present in Teradata SQL: • ABS – Stands for ‗ABSOLUTE‘ and it computes the absolute value of an argument passed into it. would result in just the number 6.

57. would not result in any error. • LN — Computes the natural logarithm of the argument.71828182845905.38905609893065E 000. where e = 2. SELECT 5/ NULLIFZERO (0).00000000000000E 000. . you would not see all the formatting displayed.566(FORMAT ‗999.30102999566398E 000 • NULLIFZERO – If you want to avoid errors with division by zero. SELECT ZEROIFNULL (argument). would display (in BTEQ session) the value 123. • EXP — Raises the base of natural logarithms (e) to the power of the argument. SELECT RANDOM (1. would result in random values from 1 to 100.38905609893065E 000) would result in 2. USE BTEQ instead to see proper output. How to Create a Stored Procedure in Teradata? As mentioned earlier also you might not be able to see the correct output If you are using Teradata SQL Assistant. SELECT LN (7.100).value you can make use the FORMAT function described earlier.99‘). • SQRT – This function is used to compute the square root of an argument. SELECT EXP (2) would result in7. use this function which converts data from zero to null hence you would not get ‗2618: Invalid calculation: division by zero‘ error. SELECT 123. Log (20) ———————1. For Example SELECT SQRT (9) would display the value 3. • ZEROIFNULL – This function is used where in case the value of a column might return a NULL but we still need to do an arithmetic calculation and avoid any error. • LOG – This function computes the base 10 logarithm of an argument. Following is the syntax. For example SELECT LOG (20) would give the following result. • RANDOM — Returns a random integer number for each row of the results table from a given lower limit and an upper limit.

Thus it acts as an interface in between SQL Assistant and Teradata DB. 8. What does this mean? It is simple.If there is anything lacking in SQL to make it a complete programming language. INOUT errstrVARCHAR(30)) BEGIN INSERT INTO Employee (EmpNo. It is stored in USER DATABASE space. 3. 4. at this time the Stored Procedure itself becomes the server to the SQL Assistant and client to Teradata DB. But when you call a Stored Procedure from SQL Assistant. Where as compound statements can have multiple statements but it has to be within the BEGIN and END block. Stored Procedures are written in SQL and consist of a set of control and condition handling statements. excluding Cursors. Can have parameters like IN. SELECT DeptName INTO dept_name FROM Department . when you use Teradata from any client software such as SQL Assistant. 5. DeptNo ) VALUES (emp_number. 2. Stored Procedure is a database ‗Object‘ and DDL is used to create it. Stored Procedures fulfills the same. IN dept_number INTEGER. Single statement procedure can contain only one looping mechanism or SQL statement. OUT and INOUT. 1. 7. Stored Procedure body can be either a single state a compound statement. It is not a good practice to use DDL statements within a Stored Procedure. Things to NOTE while creating a Stored Procedure. dept_number). OUT dept_nameCHAR(10). These features provide a server-based procedural interface to the Teradata Database for application programmers. The set of statements that forms the primary task of a Stored Procedure contains within. what is called a Stored Procedure body. 6. then Teradata becomes the server and the SQL Assistant becomes the client. Example: CREATE PROCEDURE MyFirstProc (IN emp_number INTEGER.

As already mention in this article that we do not have any such similarities present in Teradata but the same can be implemented in some or other ways. 211). select * from emp where rownum<=3.set retlimit 3 . END.WHERE DeptNo = dept_number.set retcancel on select * from emp order by emp_id. To execute/call the above Stored Procedure: CALL MyFirstProc (495. emp_id startdate name *** Warning: RetLimit exceeded. In this article we are going to discuss how to limit the output rows in BTEQ environment. 3 columns returned. 10 rows found. . This command has to be used with the ―set retcancel‖ to stop query processing when the limit is reached. *** Query completed. To know more on how to login to BTEQ visit this link. In BTEQ we have a SET command to limit the number of rows of output returned to the terminal/file. It is ―set retlimit<number>‖. ———– ——— —————————— 1 2 3 07/10/17 Bill 07/10/17 Dan 07/10/17 Harry Cancelling the request. Following results would be displayed on the terminal. Logon to BTEQ session and type in the following commands one after another. Suppose we have the following command present in Oracle and we need to have the same results when we run it in Teradata. ORDER BY clause mentioned in the above query is optional. *** Total elapsed time was 1 second. How To Limit Number Of Rows Output in BTEQ? If you are coming from an Oracle background then you must be used to the features of the pseudo column rownum. Create a Volatile table and insert a few rows as mentioned in this post. String Manipulations in Teradata SQL .

4. It also can be a byte or a numeric constant. FLOAT. select char2hexint („a‟) would result in the value „0041‟.LOWER => Used to display character string all in lower case. 2.Written by admin in Wednesday. select („name in upper‟) would return as „name in upper‟. 5. This is not ANSI compatible. If the arguments are CHAR then the result type would also be CHAR. „a‟) or select position („a‟ in „name‟). BYTE includes VARBYTE and BLOB and numeric includes DECIMAL. select index („name‟. Concatenation Operator (‗||‘) =>To concatenate two or more strings. . SMALLINT etc. 3. 1.CHAR2HEXINT=> if you want to know the hexadecimal value of a particular character then this is the function to use.Example select „firstname‟ || „ „ || „lastname‟ as full_name. While CHAR string includes VARCHAR and CLOB. the result type is BYTE. Would result in the same output as 2. Following is a small list of string manipulators present in Teradata. hence it is better to use the POSITION function in such case.POSITION => Returns the position of 2nd argument where 1st argument starts. In other words it returns a string that is identical to argument passed but in lower case. August 8th 2007 under IT 1 Comment received When we speak about a ―String‖. Would result in Full_Name FirstNameLastName Result type of the concatenation depends on the types of argument in the expression. numeric or a character string or string expression(s). the result type would still be a CHAR and in the case of BYTE. a string can be a byte.INDEX => This one needs two arguments and returns the position in 1 stargument where the 2nd argument (string expression) starts. it does not necessarily have to be a character strin g. In the case of NUMERIC. INTEGER.

This would give output as ―11/06/2007″. 7. this function is used to remove extra spaces in any character or byte string expression.UPPER => Used to display character string all in UPPER case. How to use EXTRACT function with date and time columns Written by admin in Tuesday.SUBSTRING/SUBSTR => Extracts a substring from a named string based on position. select („lower‟) would return as „lower‟. would result in the same value as T500. To see the current date we use select date. select soundex („toam‟).SOUNDEX => Returns a character string that represents the Soundex code for an argument passed.TRIM => As the name suggests. Here is the syntax.select position („a‟ in „name‟). trim (both | leading |trailing { trim_character } from {string_expression}) 9. Suppose you want to EXTRACT the month from the date then use this query select extract (month from date). This Soundex is a system that codes surnames having the same or similar sounds. In other words it returns a string that is identical to argument passed but in UPPER case. 6. This would give ―11″ as the output. Similarly for YEAR and DAY use the following commands . Let‘s try this out with system date and system time commands. Here is the syntax Select substr (<column name> from <starting position> for <ending position>) 8. Example selectsoundex („tom‟). would return 2. November 6th 2007 under IT 4 Comments received Using Extract function with any date field is easy. but different spellings.

But for HOUR MINUTE and SECONDS extractions. First lets create a sample table calledemp which should have rows like the following. emp_no 7000 4000 5000 2000 1000 6000 3000 8000 9000 1100 dept_no 114 113 114 112 112 115 111 113 115 114 first_name Read John Robert Larry Philip Tony Micheal Philip Tony Micheal last_name Vitamin Row Howard King Josheph Peter Reed Josheph Peter Reed salary 62. If you use anything else than the expected inputs for the EXTRACT function you would end up getting 3707: Syntax error.000 60.000 65. use the following commands select extract (minute from time). select extract (hour from time) .000 72. October 25th 2007 under IT 3 Comments received In this Section we are going to try out all possible simple SELECT queries. select extract (day from date).000 46. we have to use time instead.000 72. To see time use the following command.000 65.000 65. select time. This would return ―15:59:19″ as the output and to extract HOUR MINUTE and SECONDS from this. select extract (second from time) . we can not use date command. SELECT Query with Examples Written by admin in Thursday.select extract (year from date).000 Follow this command to create a temporary table: .000 55.000 46.

'howard‟.000 72.111.65000).115.113. “salary” integer ) primary index ( “emp_no” ) on commit preserve rows. insert into emp values (1000.'vitamin‟. checksum = default. Insert values into this temp table using the following insert commands.62000).000 55.'reed‟.‟philip‟.000 6.113.‟john‟.112. insert into emp values (1100.000 4.‟micheal‟.000 9.60000).000 60.‟tony‟.'peter‟.000 65.000 65.‟larry‟.114.65000).'king‟.46000).46000).'josheph‟.000 5.'reed‟. insert into emp values (8000.000 8.‟philip‟. log ( “emp_no” integer.72000).114. insert into emp values (2000.114.000 72. “last_name” char (20) character set latin not casespecific. insert into emp values (4000.112.‟tony‟. 1. insert into emp values (6000.72000).create set volatile table emp.‟read‟.000 65. insert into emp values (3000. insert into emp values (5000. insert into emp values (9000.'josheph‟. no fallback.000 2.65000).115.000 3.000 1.55000).000 dept_no 114 113 114 112 112 115 115 113 111 first_name Read John Robert Larry Philip Tony Tony Philip Micheal last_name Vitamin Row Howard King Josheph Peter Peter Josheph Reed salary 62. “dept_no” varchar (10).‟micheal‟.‟robert‟. “first_name” char (20) character set latin not casespecific.'row‟. Result:emp_no 7. insert into emp values (7000.'peter‟.000 . Selecting all columns and all rows select *from emp.000 46.

000 1.000 72.000 7. Selecting with DISTINCT clause.100 5.000 3.000 62. Result :emp_no 1.000 60. dept_no.000 65. first_name.000 2. salary from emp where dept_no = 114 group by emp_no.100 114 Micheal Reed 46. last_name.000 46.000 8.1.100 2. salary Result:- .000 5.000 4.000 Note: – When you use DISTINCT * rows would be ordered 3.000 7.000 62. dept_no.000 6.000 46.000 65.000 dept_no 112 114 112 111 113 114 115 114 113 115 first_name Philip Micheal Larry Micheal John Robert Tony Read Philip Tony last_name Josheph Reed King Reed Row Howard Peter Vitamin Josheph Peter salary 65. first_name.000 72.000 65.000 55.000 9.000 4. Result:emp_no 1. Selecting with WHERE Clause select distinct * from emp where dept_no = 114 . select distinct *from emp.000 dept_no 114 114 114 first_name Micheal Robert Read last_name Reed Howard Vitamin salary 46. Selecting with GROUP BY Clause selectemp_no. last_name.

last_name.4.As seen above with the GROUP BY clause while using ORDER BY clause you have the option of either mentioning column names or positional value of the columns as follows: select emp_no. dept_no.3.2.100 5.000 1.000 Note :.000 7.000 65.100 dept_no 114 114 114 first_name Read Robert Micheal last_name Vitamin Howard Reed salary 62.4.5 Result:emp_no dept_no first_name last_name salary . first_name. Selecting with GROUP BY and ORDER BY Clause selectemp_no.emp_no 7.000 62. last_name.000 65.000 5.5 order by emp_no.2.3.3.When you use GROUP BY. dept_no. first_name.000 46. first_name. salary from emp where dept_no = 114 group by 1. salary from emp where dept_no = 114 group by 1.5 5.4. dept_no.4.000 dept_no 114 114 114 first_name Micheal Robert Read last_name Reed Howard Vitamin salary 46.2. dept_no.3. last_name.5 order by 1. salary Result:emp_no 1.2. last_name. You have to mention column names in the SELECT query and while GROUPING you have the option of either mentioning column names or positional value of the columns as follows selectemp_no. salary from emp where dept_no = 114 group by 1. first_name.000 Note :.

3.100 5. dept_no.000 62.000 dept_no 114 114 114 Actual Salary 46.4 order by 1.000 7. (salary * 80/100) “proposed salary” <== This is a derived Column from emp where dept_no = 114 group by 1.600 . we generally create a column on the fly with the mathematical calculation from the existing salary column.2.000 65. selectemp_no.100 5.000 Proposed Salary 36.000 What is a Derived Column Written by admin in Friday. For example. the HR head decided to reduce the salary by 20% of all the employees just to show that he is something. 2 Result:emp_no 1. Suppose in a well managed successful organization.1.800 52.000 7.000 49. we have to use a derived column as mentioned in the following query. in our table we have salary column present but when we want to extract a percentage of the salary or portion of the salary.000 114 114 114 Micheal Robert Read Reed Howard Vitamin 46. salary “actual salary”. October 26th 2007 under IT Leave your reply A derived column is a result of a calculation (most of the time mathematical) that happens during the SQL query processing and generates a new column which is not part of the original table definition.000 65. To know the future salary of the employees working in the dept 114.000 62.

Sign up to vote on this title
UsefulNot useful