Queries are the meat of a TriNetX study. A study can have one query or many. They can be almost identical but run on different networks, age groups or time periods, or they can be completely different from one another.

Once a query is built, the TNX analytics features help you gain insights. Those insights might encourage you to go ahead with a research project, edit your query, or take your research in a new direction.

What is a query in TNX?

A query asks for the details of the patient population you specify. You define that population with Must Have and Cannot Have criteria, just like the inclusion and exclusion criteria in a written study protocol. The patients who fit your criteria are your cohort.

A query in the Query Builder with the Must Have and Cannot Have columns highlighted.

Build a basic query

Click New Query New Query button in the upper right of the Query Builder to add a brand-new query to the study.

Choose a network

At the top of your query in the Query Builder, you select a data network. Click the network name to see your other choices.

The top of a query with the network name, Stony Brook University, highlighted.

All networks let you run analyses on your query's cohort, but not all of them allow dataset downloads. Each query in a study can use a different network, and you can easily switch a query to another network if you change your mind. See Networks in TriNetX.

Three networks, Research, Stony Brook University and US Collaborative Network, with their Connect, Analytics and Data sets icons highlighted.

Choose filters

Query results can be filtered by medical center type, data types, and age and sex.

  • Medical center type: HCOs sharing data can be Academic, Non-Academic or Unknown.
  • Data types: HCOs sharing data must have the types you select: Demographics, Diagnoses, Genomics, Labs, Meds or Procedures.
  • Age and sex: select for patients' sex or current age. The age reflects each patient's current age on the day you count the patients in your query.
The Query Filters panel with Medical Center, Data, and Age and Sex settings, beside a population chart by age and sex.

Find and add terms

Use the search boxes to find your criteria. You can enter known codes, or text if you don't know the code. General text lets you narrow things down from the results. For example, here is a text search for "Liver".

Search results for "Liver" in the Must Have box, listing diagnoses and labs with patient counts, and the Add To Query button highlighted.

The results include diagnoses, labs, procedures, medications and genomics. Tip: to see only diagnoses, click the highlighted Diagnoses filter button.

Drill down to a more specific liver disease diagnosis by hovering over the term, then clicking the blue tree for that code.

The View term hierarchy tooltip over the blue tree icon on a term.

Once a tree is open, you may also see codes with blue sideways arrows sideways arrow. Click those to expand them and show subcodes.

Tick the terms you want, then click Add to Query. New terms appear on the Must Have or Cannot Have side, wherever you searched. Lab terms let you enter values, and choose whether the most recent lab result must meet them or any value in the patient's history. You can drag terms from one side to the other as needed.

Must Have terms joined by OR and AND, with a lab term for anticoagulants below.
Important: Pay attention to the ANDs and ORs. Must Have terms are joined by AND by default, and Cannot Have terms by OR, but you can click the word to switch between them.

Count patients and name your query

Click Count Patients, the rounded arrows Count Patients icon, to see how many patients in your chosen network fit your criteria.

Name each query so you can find it again among your other queries. Click the pencil to edit the name at any time.

An Unnamed query with the pencil icon highlighted.

Timesaver tip: don't rebuild your query!

You can import a query from any study you created, that was shared with you, or that is a template in the system, even the study you are in now. In a new blank query, select Import Query. Pick the study with the query you want, then choose the query to import it as your new query. This saves effort when you just want to edit the original.

A blank query with the Import Query button highlighted.
Note: Every time you edit an existing query and count again, a new unnamed query is created and added to the growing list on the right of your screen. This ever-expanding pile can get confusing. Delete junk queries as you work, and name the ones you want to track. You can also click the star on favorite queries to find them easily.

Build a complex query

Real study questions are rarely simple. Here are some more advanced tricks for fine-tuning your queries.

Filter a single term

The funnel on a term lets you add filters to it, such as Age at Event, or details about cancer, medications or labs. For example, you can require patients in your cohort to have been a certain age when something happened in their health history.

A Must Have term, Major depressive disorder, recurrent, with the funnel icon highlighted and the tooltip "Add term filters".
The Age at Event filter set to between 35 and 50 years.

The power of the group

Groups help you organize and fine-tune your query. Use them to set time constraints, require a number of instances or unique instances of terms, and create time relationships between two related groups.

Click Create a New Group at the bottom of the Query Builder page. Terms must first be searched and added to your main query from the main search box. Then use the Add Terms button to pick from a checklist of your query's ungrouped terms.

The Create a New Group button.

Group Rules

Click Group Rules in your group box to set any of these: number of instances, number of unique instances, a related group, or a time constraint.

The Group Rules menu: Add number of instances, Add number of unique instances, Add related group and Edit time constraint.

Add a time constraint

You can filter for when terms happened. Click Time Constraint and set a Relative Time (say, within the last 3 months) or Specific Dates (maybe March 1, 2020 to March 1, 2021). You can have several groups, each with its own constraint.

The Time Constraint for Group 1 with Relative Time selected, set from 3 months before to today.

The constraint applies to every term in that group. Terms not moved into the group are not affected.

A group named HepB Vaccine, limited to terms that occurred between March 1, 2020 and March 1, 2021.

Create a related group

You may want patients who had your terms in a time relationship to other terms. Create a group for your first terms, then click Add Related Group in Group Rules. Now you have groups A and B. Add the terms you want to relate to group A into group B, then click to set the relationship.

The bar "Set a relationship between groups before running the query" with the Set Relationship button.

Imagine group A stays firmly in place. Group B can move ahead of or behind group A by the time period you set.

You can also set the instance type for each group. For example, if your term were a myocardial infarction, you might choose the Most Recent Instance, or the First Instance.

Setting a relationship between 1A and 1B: any instance of 1B occurred within 3 years before any instance of 1A, with Any instance, Most recent instance and First instance options for each group.
Hot tip! You can add the same term to a query more than once to capture all the relationships between terms. In this example, cellulitis is related both to a history of diabetes and to an inpatient admission. For a full walk-through, see the query build example.
Two groups using the term Cellulitis and acute lymphangitis twice: Group 1 relates it to a prior type 2 diabetes diagnosis, and Group 2 relates it to an inpatient encounter.