Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
Query tables
Tables can be combined with query expressions, unlike maps. The basic type of the output of a query expression is determined by the contextually expected type, and the input type.
import ballerina/io;
type Employee record {|
readonly int id;
string firstName;
string lastName;
int salary;
|};
public function main() {
table<Employee> key(id) employees = table [
{id: 1, firstName: "John", lastName: "Smith", salary: 100},
{id: 2, firstName: "Jane", lastName: "Smith", salary: 150},
{id: 4, firstName: "Fred", lastName: "Bloggs", salary: 200},
{id: 7, firstName: "Bobby", lastName: "Clark", salary: 300},
{id: 9, firstName: "Cassie", lastName: "Smith", salary: 250}
];
// Since the contextually-expected type for the query expression is `int[]`,
// the evaluation of the query expression will result in an integer array.
int[] salaries = from var emp in employees
select emp.salary;
io:println(salaries);
// The query expression creates a table based on the contextually-expected type.
table<Employee> highPaidEmployees = from Employee emp in employees
where emp.salary > 180
order by emp.firstName
select emp;
foreach Employee emp in highPaidEmployees {
io:println(emp.firstName, " ", emp.lastName);
}
}
$ bal run querying_tables.bal[100,150,200,300,250]Bobby ClarkCassie SmithFred Bloggs
Related links
PreviousOuter Join clause
NextCreate tables with a query