How to retrieve Data from the database in LWC?

Lightning Web Components (LWC) are used to create custom screens in Salesforce. Those custom screens can be used on Record Pages, Home Pages, App Builder Pages, Experience Builder Sites, and Custom Actions.

Sometimes we need to retrieve some data from the database and show them on the screen.

To retrieve values of some fields when you have the record id

Example: Get the Account Name of an Account when you have the record id.

import { LightningElement, api, wire} from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';

// Field Imports
import ACCOUNT_NAME from '@salesforce/schema/Contact.Account.Name';

const fields=[ACCOUNT_NAME];

export default class accountLwc extends LightningElement {
    
@api recordId;

    //Account Name will be retrieved and stored in this variable
    accountName; 

    @wire( getRecord, { 
        recordId: '$recordId', 
        fields 
    })record({data, error}){
        if (data) {
            this.accountName = getFieldValue(data, ACCOUNT_NAME);
            console.log(this.accountName);
        }else{
            console.log(error);
        }
    }
}

Similarly we can also include fields from a Lookup field.

import PARENT_ACCOUNT_NAME from '@salesforce/schema/Cont

To retrieve a list of records using Wire

Example: